Use Storyboard to mask UIView and give rounded corners?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
For a simple UIView, Storyboard can handle rounded corners without much code by setting layer-backed properties through User Defined Runtime Attributes. That is enough for the common case where you want all corners rounded and the view to clip its content. The moment you need selective corners, dynamic sizing, or a custom mask shape, Interface Builder usually needs help from code.
The Simple Storyboard-Only Setup
A UIView is backed by a CALayer, and rounded corners are really a layer setting. In Interface Builder, you can set that using User Defined Runtime Attributes.
Add these attributes to the view:
- key path
layer.cornerRadiuswith typeNumber - key path
layer.masksToBoundswith typeBoolean
That gives you the same effect as the following code:
For many card-like views, that is enough.
What masksToBounds Actually Does
cornerRadius changes the shape of the layer's visible corners. masksToBounds clips subviews and layer contents to that rounded shape.
That means if you have an image or colored subview inside the rounded container, it will stay inside the rounded boundary once masking is enabled.
Without masking, the corners may look rounded on the outer layer while child content still draws square edges beyond the curve.
Example View Controller Code
Even if the effect was configured in Storyboard, it helps to understand the equivalent runtime code.
If Storyboard already sets the values correctly, you may not need this code. But code is often easier to version, review, and adjust dynamically.
When Storyboard Is Not Enough
Storyboard becomes limited when you want:
- only some corners rounded
- a custom mask path
- a mask that depends on final Auto Layout size
- rounded corners plus a shadow on the same view
For selective corners, modern iOS APIs allow maskedCorners:
viewDidLayoutSubviews is a good place for this because the final view bounds are available there.
Rounded Corners and Shadows Need Two Views
A common frustration is that masksToBounds = true clips shadows away. If you want both rounded corners and a visible shadow, use two views:
- outer view draws the shadow
- inner view clips to rounded corners
Trying to make one view do both jobs usually leads to disappointment because clipping and shadow rendering pull in opposite directions.
Custom Masks
If you need a non-rectangular shape, use a CAShapeLayer as the mask.
This goes beyond what Storyboard alone can express cleanly.
Common Pitfalls
The most common mistake is setting cornerRadius but forgetting to clip the contents. The outside looks rounded, but inner content still spills past the corners.
Another mistake is expecting Storyboard to handle dynamic corner logic for a view whose size depends on Auto Layout. In those cases, runtime code is more reliable.
Developers also often try to combine rounded corners and shadows on the same masked view. That clips the shadow away.
Finally, if only some corners should be rounded, do not fight Interface Builder too long. Use maskedCorners or a shape-layer mask in code.
Summary
- Storyboard can handle simple rounded corners through layer runtime attributes.
- Use
cornerRadiusplusmasksToBoundsfor the common all-corners case. - Move to code when you need selective corners, dynamic sizing, or custom shapes.
- Use separate views when you want both rounded corners and a shadow.
- Prefer
viewDidLayoutSubviewsfor masks that depend on final view bounds.

