1

The class given below is working fine

@IBDesignable class iButton : UIButton {

@IBInspectable var cornerRadius : CGFloat = 0.0{
    didSet{
        layer.cornerRadius = cornerRadius
    }
}}

But question is, when i set cornerRadius value 35 in Attribute Inspector for the button with size of (width : 70, Height 70). Im getting rounded button on the storyboard but while running it on Simulator its not circular instead its rounded rectangle.

My Design View on xCode is iPhone-SE and simulated on iPhone-7-plus simulator.

I have also enabled autoresize by setting the height and width on the Size Inspector.

Check this Image for xcode design and Simulated device Screen

Size Inspector for Auto-Resizing

Out of my knowledge the corner radius should be the half of the width. And when the button is resized by auto-resizing why not the corner radius is resized. how can i fix this?

Thanks in advance.

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
Hurdler
  • 7
  • 3

2 Answers2

2
import UIKit

@IBDesignable 
class RoundedCornerButton: UIButton {

    override func drawRect(rect: CGRect) {
        self.clipsToBounds = true
    }

    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            self.layer.cornerRadius = cornerRadius
        }
    }
}

or you can check this link this link for more info

https://www.youtube.com/watch?v=JQ5i2YKwvJ8

rnevius
  • 26,578
  • 10
  • 58
  • 86
Subhojit Mandal
  • 450
  • 4
  • 13
-2

Thank You i got answer.

We also need to scale the cornerRadius.

layer.cornerRadius = cornerRadius * (UIScreen.main.bounds.width/320.0) //320.0 be my width of the screen that i designed on storyboard
Hurdler
  • 7
  • 3