Layer.corner radius top left on storyboard not working
Starting from Xcode 6, you can build custom UI and have it rendered real time in Storyboard / XIB / Interface builder. Show We are going to create an UIView with rounded corners and borders in this post. First, create a UIView subclass, I name it as 'RoundedCornerView' And add the following cornerRadius attribute to the newly created subclass file. RoundedCornerView.swift
The above snippet initialize cornerRadius variable to 0 and any change on its value (didSet) will update the view layer cornerRadius and masksToBounds boolean if the cornerRadius is larger than 0. The @IBInspectable keyword in front of the variable lets you set the value of cornerRadius in Interface builder! Since @IBInspectable is based on User-defined runtime attributes, the variable types it support are also same as the runtime attributes : Boolean, Numbers (NSNumber, double, CGFloat etc), String, CGPoint, CGSize, CGRect, UIColor (Not CGColor), UIImage and also NSRange. Select your desired view in interface builder, set the class to RoundedCornerView. Now select Attribute Inspector tab, you should see the corner radius attribute there, Xcode even auto convert your camelcased variable name to proper format! You can now set the corner radius of the view in Interface Builder, but the view still looks square after setting the corner radius to 40, what did we miss? 🤔 You need to add @IBDesignable on top of the class definition to tell Xcode that it needs to render the view properties directly in the Interface builder. RoundedCornerView.swift
After adding Praise rounded corners, now you can do the same for borders : RoundedCornerView.swift
Result : You can download the sample Swift project here Tired of fighting with Auto Layout constraints? Why is it so hard to make a layout to work?! Would using code for UI and constraints make it easier? (No, not really) If you want to understand Auto Layout fundamentally (instead of just following youtube tutorials implementing a very specific layout which might not apply to your app), check out my book Making Sense of Auto Layout, with practical case study! You should update the mask in the override of As an aside, I’d discourage the use of I might also suggest observers on the inspectable properties, to trigger the update of the corner rounding. You want the corner rounding to update automatically if you change these properties. Thus:
Or, alternatively, if targeting iOS 11 and later, we can let CoreAnimation do the rounding:
The nice thing about this latter approach is that CoreAnimation will honor our corner rounding if we happen to be animating the If you use the aforementioned
0). How do you change the corner radius on a 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 to set corner radius for top in Swift?func roundCorners(cornerRadius:. let path = UIBezierPath(roundedRect. bounds, byRoundingCorners: [. topLeft, . topRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) let maskLayer = CAShapeLayer.. maskLayer. frame = self. bounds. maskLayer. path = path. cgPath.. self. layer. mask = maskLayer. }. How do you give a border radius to only the left side?CSS Syntaxborder-top-left-radius: length|% [length|%]|initial|inherit; Note: If you set two values, the first one is for the top border, and the second one for the left border. If the second value is omitted, it is copied from the first. What is topThe border-top-left-radius CSS property rounds the top-left corner of an element by specifying the radius (or the radius of the semi-major and semi-minor axes) of the ellipse defining the curvature of the corner. |