You can do this in a storyboard using custom properties. Select the view 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: (any desired radius) - Key path:
layer.masksToBounds , Type: Boolean, value: checked
You may need to import QuartzKit into your corresponding class file (if any), but I swear I got it to work without doing this. Your results may vary.
EDIT: dynamic radius example
extension UIView { /// The ratio (from 0.0 to 1.0, inclusive) of the view 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 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 checked that this works in the playground.
NRitH Dec 11 '15 at 3:10 2015-12-11 03:10
source share