Skip to content

Rounding the Corners of a UIView

iOS, Swift, UIView1 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:

  1. Open Your Project in Xcode: First, make sure your project is open in Xcode.
  2. Navigate to the Storyboard or XIB file: In the project navigator, find the storyboard or XIB file where your UIView is located.
  3. Select the UIView: Click on the UIView you want to round the corners of. Make sure you have selected the correct view.
  4. Open the Identity Inspector: With the 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.
  5. Add a User-Defined Runtime Attribute:
    • In the Identity Inspector, you'll see a section called "User Defined Runtime Attributes."
    • Click on the "+" button to add a new attribute.
    • For the Key Path, enter layer.cornerRadius.
    • Set the Type to Number.
    • For the Value, enter the radius you want for your corners. This number is in points, so adjust according to the size of your UIView.
  6. Enable Clipping: To make sure the view clips its contents to the rounded corners, you need to set the clipsToBounds property.
    • Add another User Defined Runtime Attribute.
    • For the Key Path, enter clipsToBounds.
    • Set the Type to Boolean.
    • For the Value, set it to YES.
  7. Run Your Project: Now you can run your project to see the rounded corners on your UIView in action.

Programmatically Rounding Corners

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 = 10
2yourView.layer.masksToBounds = true

In this example, yourView represents the instance of the UIView you wish to modify.

Applying Border and Shadow

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:

Applying a Border

1yourView.layer.borderWidth = 1.0
2yourView.layer.borderColor =

In this snippet, a black border with a width of 1 point is applied to yourView.

Adding a Shadow

1yourView.layer.shadowColor =
2yourView.layer.shadowOpacity = 0.5
3yourView.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.