Add rounded corners to all UIImageViews
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Rounded images are a common design requirement for avatars, cards, and gallery layouts. The implementation is simple once you know where to apply it, but many bugs come from doing it too early in the layout cycle or expecting a global switch that UIKit does not provide.
The Basic Layer Settings
Rounded corners on a UIImageView come from its backing layer. In practice, you need two settings:
- '
layer.cornerRadiusto define the curve' - '
clipsToBoundsorlayer.masksToBoundsto keep the image inside that shape'
That works for a fixed radius, but it only gives the correct result if the view already has its final size. If Auto Layout changes the bounds later, the radius may be wrong.
Apply the Radius After Layout
For circles or size-dependent corners, update the radius in layoutSubviews() or viewDidLayoutSubviews(), not in viewDidLoad().
Using bounds.height / 2 creates a circle when the image view is square. For a card image with soft corners, use a fixed value such as 12 or 16.
If You Mean Every Image View on a Screen
There is no built-in UIKit setting that automatically rounds every UIImageView in the entire app. If you need to update every image view inside one screen or container, walk the view hierarchy and style the matching views.
This approach is useful when you are cleaning up an existing screen and do not want to replace every outlet. It is less ideal as a long-term pattern because it applies one visual rule to everything it finds.
If You Mean Every Image View You Create Going Forward
The cleaner solution is usually a subclass. A subclass makes the style explicit, works in Interface Builder, and avoids surprising unrelated image views.
Now you can assign RoundedImageView in Interface Builder or create it in code. That gives you a reusable component instead of a one-off visual fix.
A Note About Borders and Shadows
Borders work well with rounded images because they are also layer properties:
Shadows are different. If you set clipsToBounds = true, the shadow is clipped away. The usual solution is to put the UIImageView inside a container view. Apply the corner radius to the image view and the shadow to the container.
That separation keeps the image masked while still letting the outer shadow render correctly.
Choosing the Right Pattern
Use the simplest option that matches the scope of the problem:
- one image view: set the layer directly
- many image views in one screen: traverse the hierarchy or style them in a helper
- repeated app-wide design pattern: create a subclass
Trying to force a global rule onto plain UIImageView instances usually creates maintenance problems. A named subclass is easier to reason about and makes the design intent obvious to other developers.
Common Pitfalls
- Setting
cornerRadiusbefore Auto Layout has produced the final bounds, which leads to the wrong shape. - Forgetting
clipsToBoundsorlayer.masksToBounds, so the image still draws past the rounded edge. - Expecting
UIImageView.appearance()to solve this globally, even though the relevant properties live on the layer. - Using a circular radius on a non-square image view and wondering why the result looks oval or clipped.
- Applying shadows directly to the same view that clips its content, which hides the shadow.
Summary
- Rounded corners on
UIImageViewcome fromlayer.cornerRadiusplus clipping. - Apply size-dependent radii after layout, not in
viewDidLoad(). - For existing screens, you can walk the view hierarchy and style every image view you find.
- For long-term reuse, a
RoundedImageViewsubclass is the cleaner solution. - Put shadows on a container view when the image itself needs clipping.

