— iOS, Swift, UIView — 1 min read
One of the simplest ways to add rounded corners to a UIView
is by utilizing Interface Builder, part of Xcode's integrated development environment. Here's a step-by-step guide:
UIView
is located.UIView
you want to round the corners of. Make sure you have selected the correct view.UIView
selected, open the Identity Inspector pane on the right side of the Xcode window. You can do this by clicking on the third icon from the left in the toolbar at the top of the right panel.layer.cornerRadius
.Number
.UIView
.clipsToBounds
property.clipsToBounds
.Boolean
.YES
.UIView
in action.While Interface Builder provides a convenient way to set corner radii visually, you may also want to achieve the same effect programmatically. This approach allows for more dynamic adjustments and can be useful when working with views created in code or when the corner radius needs to change during runtime.
To round the corners of a UIView
programmatically, you can use the following Swift code:
1yourView.layer.cornerRadius = 102yourView.layer.masksToBounds = true
In this example, yourView
represents the instance of the UIView
you wish to modify.
In addition to rounding the corners, you might want to apply a border or shadow to the UIView
. Here's an example of how to do this:
1yourView.layer.borderWidth = 1.02yourView.layer.borderColor = UIColor.black.cgColor
In this snippet, a black border with a width of 1 point is applied to yourView
.
1yourView.layer.shadowColor = UIColor.black.cgColor2yourView.layer.shadowOpacity = 0.53yourView.layer.shadowOffset = CGSize(width: 0, height: 2)4yourView.layer.shadowRadius = 3.0
These properties create a subtle black shadow around yourView
, enhancing its visual presentation.