Swift version: 5.6
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. So if you have a view that's 100x100 points and give it a cornerRadius
property of 50, 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.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Available from iOS 3.2
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.