Lots of questions like this explain how to programmatically create a mask and provide rounded corners to a UIView.
Is there a way to do it all within Storyboard? Just asking because it seems like creating rounded corners in Storyboard keeps a clearer demarcation between presentation and logic.
If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.
Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)
The formula for it is the height of button * 0.50. So play around the value to see the expected rounded button in the simulator or on the physical device.
Setting the corner radius to 100 will make the image view completely rounded. Try different corner radius like 10, 20, 30, 40 to get image corners of different radius.
Yes, I use that a lot but question like this was already answered many times.
But anyway in Interface Builder. You need to add User Defined Runtime Attributes like this:
layer.masksToBounds Boolean YES layer.cornerRadius Number {View's Width/2}
and enable
Clips subviews
Results:
You can do it in a storyboard by using user-defined properties. Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries:
layer.cornerRadius
, Type: Number, Value: (whatever radius you want)layer.masksToBounds
, Type: Boolean, Value: checkedYou may have to import QuartzKit
in your view's corresponding class file (if any), but I swear that I've gotten it to work without doing that. Your results may vary.
EDIT: Example of a dynamic radius
extension UIView { /// The ratio (from 0.0 to 1.0, inclusive) of the view's corner radius /// to its width. For example, a 50% radius would be specified with /// `cornerRadiusRatio = 0.5`. @IBDesignable public var cornerRadiusRatio: CGFloat { get { return layer.cornerRadius / frame.width } set { // Make sure that it's between 0.0 and 1.0. If not, restrict it // to that range. let normalizedRatio = max(0.0, min(1.0, newValue)) layer.cornerRadius = frame.width * normalizedRatio } } }
I verified that this works in a playground.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With