Reputation: 1385
I would like to create a circular button in an UITableViewCell
.
I am using autolayout
constraints in IB.
Generally, I set the corner radius in viewDidLayoutSubviews
to keep the buttons circular despite the autolayout
.
I have not found how to do that in an UITableViewCell
. My buttons are diamond shape instead of oval ! :-(
Thanks !
here is how I make a button circular :
extension UIButton {
func setCircular() {
layer.cornerRadius = layer.frame.width / 2
}
}
I do that in
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCellClass
cell.goodButton.setCircular()
}
Upvotes: 1
Views: 1505
Reputation: 732
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
I use this extension in swift to set corner radius via Interface Builder. Hope it helps
Upvotes: 0
Reputation: 1213
This will help you:
goodButton.layer.cornerRadius = goodButton.layer.frame.height / 2
goodButton.layer.masksToBounds = true
Note: Please make sure your button has same width and height.
Upvotes: 0
Reputation: 82769
I think you forget to add self.imageView.layer.masksToBounds = true
on your class
override func layoutSubviews() {
super.layoutSubviews()
self.makeItCircle()
}
func makeItCircle() {
self.yourbutton.layer.masksToBounds = true
self.yourbutton.layer.cornerRadius = CGFloat(roundf(Float(self.yourbutton.frame.size.width/2.0)))
}
for additional information see this
Upvotes: 4
Reputation: 1606
If your Button height width is fixed you can use User Defined Runtime Attributes this will directly set corner radius from interface builder
Reference link User Defined Runtime Attributes
Upvotes: 1