How to set cornerRadius for only top-left and top-right corner of a UIView?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Setting the cornerRadius for only the top-left and top-right corners of a UIView in iOS development can significantly enhance the user interface design with rounded appearances. Unlike a simple cornerRadius applied to all corners, specifying corners requires use of additional techniques. Here's a comprehensive guide on how to achieve this selectively in Swift.
Understanding UIView and Its Layer Property
Every UIView in iOS has an associated CALayer. This layer allows for more granular control over visual properties, including applying rounded corners, borders, shadows, and more.
The cornerRadius Property
The cornerRadius property on a UIView's layer provides a way to round all the corners of the view. However, when you want to apply this only to specific corners, you'll need to combine cornerRadius with maskedCorners, introduced in iOS 11.
Applying cornerRadius to Specific Corners
Here is how you can apply a cornerRadius to only the top-left and top-right corners of a UIView.
Step-by-Step Guide
1. Import Necessary Modules
First, ensure you have the necessary imports in your Swift file:
2. Configure the UIView
When configuring your view, you'll want to access and modify its layer property directly. Below is an example of how this can be done:
Explanation:
layer.cornerRadius: Specifies how rounded the corners should be.layer.maskedCorners: Takes an array of corners to apply the radius to.CALayerprovides constants for each corner (.layerMinXMinYCorner,.layerMaxXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMaxYCorner).layer.masksToBounds: Ensures that the subviews and the layer itself are clipped to the bounds of the view including corner radius.
Compatibility
This approach requires a minimum deployment target of iOS 11. For iOS versions prior to 11, you would need a more complex approach involving UIBezierPath and CAShapeLayer.
Advanced Considerations
Using UIBezierPath for Precise Customization
For increased customization or compatibility concerns when dealing with layers and path-based manipulation, UIBezierPath combined with CAShapeLayer can offer a powerful alternative.
Example:
Usage:
Key Considerations
- Apply the mask in
layoutSubviews, as the view's bounds are fully calculated and correct at this point. - Any dynamic resizing may require updating the mask if view dimensions change.
Summary Table
| Concept | Description | iOS Version |
layer.cornerRadius | Rounds all corners of the view's layer | iOS 2.0 and later |
layer.maskedCorners | Customizes which corners to round | iOS 11.0 and later |
layer.masksToBounds | Ensures content and subviews are clipped | iOS 2.0 and later |
UIBezierPath & CAShapeLayer | Advanced customizability of corner rounding | All iOS versions with Core Graphics |
This approach to customizing corner radii allows developers to bring subtle yet impactful enhancements to app design, ensuring a consistent and pleasing UI/UX experience across devices and orientations.

