Use a storyboard to hide a UIView and give rounded corners?

Many questions like this explain how to programmatically create a mask and provide rounded corners in a UIView.

Is there a way to do all this in the storyboard? Just ask, because it seems that creating rounded corners in the Storyboard provides a clearer demarcation between presentation and logic.

+86
ios xcode autolayout swift storyboard
Dec 11 '15 at 2:47
source share
4 answers

Yes, I use it a lot, but a question like this has already been answered many times.

But anyway in Interface Builder. You need to add User Defined Runtime attributes as follows:

layer.masksToBounds Boolean YES layer.cornerRadius Number {View Width/2} 

enter image description here

and turn on

 Clips subviews 

enter image description here

Results:

enter image description here

+230
Dec 11 '15 at 3:11
source share

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.

+19
Dec 11 '15 at 3:10
source share

Choose view you changed Corner Rediuse, Border Widthe and Border Color also Using StoryBord

 extension UIView { @IBInspectable var cornerRadiusV: CGFloat { get { return layer.cornerRadius } set { layer.cornerRadius = newValue layer.masksToBounds = newValue > 0 } } @IBInspectable var borderWidthV: CGFloat { get { return layer.borderWidth } set { layer.borderWidth = newValue } } @IBInspectable var borderColorV: UIColor? { get { return UIColor(cgColor: layer.borderColor!) } set { layer.borderColor = newValue?.cgColor } } } 
+6
Apr 23 '18 at 17:47
source share

Even after making all the changes to the storyboard, Woraphot's answer does n't work for me.

This worked for me:

 layer.cornerRadius = 10 layer.borderWidth = 1 layer.borderColor = UIColor.blue.cgColor 



Long answer:

Rounded corners of UIView / UIButton etc.

 customUIView.layer.cornerRadius = 10 

Border thickness

 pcustomUIView.layer.borderWidth = 1 

Border color

 customUIView.layer.borderColor = UIColor.blue.cgColor 
0
Jun 12 '19 at 9:35
source share



All Articles