Use Storyboard to mask UIView and give rounded corners?

104,461

Solution 1

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

Solution 2

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.

Solution 3

Use IBInspectable to add the properties to the Storyboard:

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
        }
    }
}

Solution 4

Even after making all changes in storyboard, Woraphot's answer doesn'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
Share:
104,461

Related videos on Youtube

Crashalot
Author by

Crashalot

Hello. My friends call me SegFault. Describe myself in 10 seconds? You know those feisty, whip-smart, sometimes funny, and occasionally charming developers who dominate StackOverflow and consider Swift/Ruby/jQuery their native tongue? Yah, I buy coffee for them.

Updated on November 05, 2021

Comments

  • Crashalot
    Crashalot over 2 years

    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.

  • Henson Fang
    Henson Fang over 8 years
    where can i add User Defined Runtime Attributes?
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat over 8 years
    As I show in first image select view then on the right pane select third icon User Defined Runtime Attributes Click item below to add key path, type, value
  • Crashalot
    Crashalot over 8 years
    Can you make the border radius dynamic so it's always 50% of the width or does it need to be a static integer?
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat over 8 years
    As far as I know, fixed value only.
  • Crashalot
    Crashalot over 8 years
    Can you make the border radius dynamic so it's always 50% of the width or does it need to be a static integer?
  • NRitH
    NRitH over 8 years
    You can, but only in code. An interesting workaround, though, is to add an IBInspectable property to your view's class that's a numeric value between 0 and 100 and indicates the percentage of the width that the radius should be. For example, a value of 50 means that the radius should be 50% of the width. Because it's a computed (not stored) property, you can even add it to an extension of UIView so that all views can have the property.
  • Crashalot
    Crashalot over 8 years
    This works on a normal UIView, or do we need to create a custom class that subclasses from UIView? We tried this on a normal UIView, and nothing happened.
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat over 8 years
    Any view you name it be UIImageView, UITableCellView, UIView, UILabel or sub class from them even view that has many subviews. Can you check your User Defined Runtime Attributes and/or Clips Subviews again.
  • Saravanabalagi Ramachandran
    Saravanabalagi Ramachandran almost 8 years
    is it possible to add TopLeft, TopRIght, BottomLeft, BottomRight radii using this method?
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat almost 8 years
    @Zeka Dran I don't know about individual value but if it has same value you can use this method to set border radius.
  • Rob Johansen
    Rob Johansen almost 8 years
    @Crashalot: Did you ever get this working on a normal UIView? I also tried this on a normal UIView and nothing happened.
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat almost 8 years
    @AlienBishop I have just tried a minute ago and it works fine. Are you missing some value in 'User Defined Runtime Attributes'. Screenshot
  • mfaani
    mfaani almost 8 years
    how do we enable 'clip subviews'? I don't see the option in the image
  • Woraphot Chokratanasombat
    Woraphot Chokratanasombat almost 8 years
    @Honey I highlight the 'clip subviews' so it's standout now
  • Cobie Fisher
    Cobie Fisher over 6 years
    @NRitH, I would be interested to see something like that. Do you think you can post the code here?
  • Code Knox
    Code Knox about 6 years
    Just FYI, if like me you came here thinking this would work on the LaunchScreen.storyboard, it does not. You will get: Error: Launch screens may not use user defined runtime attributes. Looks like if you need a round image on the LaunchScreen, you are out of options.
  • luhuiya
    luhuiya over 5 years
    why use cornerRadiusV instead of cornerRadius?
  • Ananda Aiwale
    Ananda Aiwale over 5 years
    it is set cornerRadius using the storyboard through, save the writing coding in Project
  • shim
    shim about 4 years
    This does not attempt to answer the question and is just a duplicate of your other answer here.
  • Yogesh Patel
    Yogesh Patel about 3 years
    when my height and width are not the same then it's not working. could you suggest any alternative for it? Thanks!
  • 01GOD
    01GOD almost 3 years
    Almost makes programmatic GUI seem easier as already making much of it programmatically in storyboards anyway.