iOS
Swift
UIView
rounded corners
app development

Giving UIView rounded corners

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Creating a visually appealing user interface is a fundamental aspect of iOS app development. Rounded corners on UIView components can significantly enhance the aesthetic feel of your app elements. This effect can be easily achieved through various methods in Swift. In this article, we will delve into how to add rounded corners to UIViews, explore the technical nuances, and provide practical code samples.

Methods for Creating Rounded Corners

1. Using layer.cornerRadius

The layer.cornerRadius attribute of a UIView's layer is the most straightforward way of implementing rounded corners. Setting this property will round the corners of the view accordingly.

swift
let myView = UIView()
myView.layer.cornerRadius = 10
myView.clipsToBounds = true // Ensures subviews also get clipped to the view’s bounds
  • Importance of clipsToBounds:
    To ensure that subviews are also clipped to the rounded corners, make sure to set clipsToBounds to true.

2. Using UIBezierPath and CAShapeLayer

For more complex shapes or to round specific corners, the UIBezierPath in conjunction with CAShapeLayer is more viable.

swift
1let myView = UIView()
2let path = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: [.topLeft, .bottomRight], cornerRadii: CGSize(width: 15, height: 15))
3let mask = CAShapeLayer()
4mask.path = path.cgPath
5myView.layer.mask = mask
  • Flexibility:
    This method offers flexibility in customizing which corners are rounded by using the byRoundingCorners parameter.

3. Subclassing UIView

To turn rounding corners into a reusable component, subclassing UIView is a solid approach.

swift
1class RoundedCornerView: UIView {
2    private var cornerRadius: CGFloat
3    
4    init(cornerRadius: CGFloat) {
5        self.cornerRadius = cornerRadius
6        super.init(frame: .zero)
7        commonInit()
8    }
9    
10    required init?(coder: NSCoder) {
11        fatalError("init(coder:) has not been implemented")
12    }
13    
14    private func commonInit() {
15        layer.cornerRadius = cornerRadius
16        clipsToBounds = true
17    }
18}
  • Reusable Customization:
    This allows for easy reuse across different parts of the app by simply initializing a RoundedCornerView.

Performance Considerations

When adding rounded corners to views, especially on complex UIs or UITableViewCells, be wary of performance implications. Overuse of corner rounding, particularly with complex view hierarchies, may result in animation and scrolling inefficiencies.

Optimization Tips

  • Rasterization:
    You can set the shouldRasterize property of the layer to true for better performance in scrolling views.
swift
  myView.layer.shouldRasterize = true
  myView.layer.rasterizationScale = UIScreen.main.scale
  • Avoiding Overdraw:
    Avoid using rounded corners if they are not critical to the UI experience on views that are drawn offscreen or excessively.

Key Points at a Glance

MethodUsageFlexibilityPerformance
layer.cornerRadiusSimplest method, directly modifies the layer.Limited (all corners same)High Optimum
UIBezierPath & CAShapeLayerAllows rounding selected corners, useful for custom shapes.HighModerate
Subclassing UIViewEncapsulates the rounded corner logic for reusable custom views.ReusableHigh Optimum
Optimization TechniquesRasterization and careful usage in complex view hierarchies minimises impact.N/AEssential

Conclusion

Adding rounded corners to UIView elements is a powerful technique to enhance the user interface. Depending on the requirements, developers can opt for simple cornerRadius settings, use UIBezierPath for more control, or create reusable subclasses to clean up code and improve readability. Always remember to consider performance best practices, especially in demanding UI contexts such as scrolling lists.

With the right approach, rounded corners can contribute significantly to a polished and professional-looking app design.


Course illustration
Course illustration

All Rights Reserved.