NEW! Master Swift design patterns with my latest book! >>

How to round the corners of a UIView

Paul Hudson       @twostraws

All UIView subclasses have the ability to round their corners thanks to their underlying CALayer – that's the bit that handles the actual drawing of your views. To round the corners of a view, use this code:

yourView.layer.cornerRadius = 10

The number you specify is how far the rounding should go, measured in points. This means that if you have a view that's 128x128 points wide and give it a cornerRadius property of 64, it will look like a circle.

Note that some types of view don't have clipsToBounds enabled by default, which means their corners will not round until you enable this property.

Available from iOS 3.2

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Upgrade to the premium experience

Get all 40 projects in PDF and ePub, plus exclusive content that will take your Swift learning to the next level – buy the Hacking with Swift book today!

MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 5.0/5

Click here to visit the Hacking with Swift store >>