Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Storyboard to mask UIView and give rounded corners?

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.

like image 575
Crashalot Avatar asked Dec 11 '15 02:12

Crashalot


People also ask

How do you round UIView corners?

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.

How do you set corner radius for UIView in Swift storyboard?

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)

How do you make a button rounded on a storyboard?

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.

How do I round image corners in Xcode?

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.


2 Answers

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} 

enter image description here

and enable

Clips subviews 

enter image description here

Results:

enter image description here

like image 55
Woraphot Chokratanasombat Avatar answered Oct 15 '22 11:10

Woraphot Chokratanasombat


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:

  • Key Path: layer.cornerRadius, Type: Number, Value: (whatever radius you want)
  • Key Path: layer.masksToBounds, Type: Boolean, Value: checked

You 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.

like image 42
NRitH Avatar answered Oct 15 '22 11:10

NRitH