I want to create a custom button in swift and the result is not quite satisfying. UIBezierpath is not drawing a smooth path. Please see image and code below.
let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))
After playing around a little bit, I found that moving the path down and right 1 point and shrinking the rectangle size by 1 will solve the problem. but I don't like this solution, I don't think this is the correct way of doing it. And I feel like the frame is clipping my image. Can someone help me with this??
let roundRect = UIBezierPath(roundedRect: CGRectMake(1, 1, 154, 59), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))
the whole CustomButton Subclass:
import UIKit
class DesignableButtons: UIButton {
let black = UIColor(red: 33/255, green: 33/255, blue: 33/255, alpha: 1.0) /* #212121 */
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.setTitle("Go to Google", forState: UIControlState.Normal)
self.titleLabel!.font = UIFont(name: "GT Walsheim", size: 14)
self.setTitleColor(black, forState: .Normal)
self.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
self.setTitleColor(UIColor.whiteColor(), forState: .Selected)
self.titleLabel!.alpha = 1.0
}
let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))
override func drawRect(rect: CGRect) {
black.setStroke()
roundRect.stroke()
if highlighted {
black.setFill()
roundRect.fill()
}
}
override var highlighted: Bool {
didSet {
setNeedsDisplay()
}
}
}
override func drawRect
No. It is not permitted to overridedrawRect
for a UIButton. You need to construct a background image and set the button's background image and let it do the drawing in its own way. – matt