How to set corner radius of imageView?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Setting a corner radius on a UIImageView is mostly a CALayer task. The radius itself is easy to set, but the visual effect only appears correctly when clipping is enabled and the radius is applied at the right point in the view's layout lifecycle. That is why this simple-looking change often behaves inconsistently at first.
The Basic UIImageView Corner Radius Setup
Every UIView, including UIImageView, is backed by a layer. To round the corners, set cornerRadius on that layer and enable clipping.
Without clipsToBounds = true, the image content still draws outside the rounded corners, so the radius may appear not to work.
Circular Images Need Layout-Aware Radius
If you want a perfect circle, the corner radius should be half the view's width or height. That means the final size must already be known.
This is more reliable than setting the radius too early in viewDidLoad when Auto Layout may not have sized the image view yet.
Storyboard and Interface Builder Option
If the image view is created in Interface Builder, you can still set the radius in code after outlets are connected.
This keeps the view structure in the storyboard while ensuring the rounded effect uses the final layout size.
Add Borders and Shadows Carefully
Rounded corners often come together with borders or shadows. Borders are easy because they belong to the same layer.
Shadows are different. If you enable clipsToBounds, the shadow gets clipped too. A common solution is to place the image view inside a container view: the container draws the shadow, and the image view clips the image.
That separation avoids fighting layer behavior.
Performance and Reuse Considerations
Rounded corners are cheap in normal UI work, but repeated layer styling across many list cells should still be centralized. A helper method or custom image view subclass keeps the style consistent.
Then use avatarImageView.applyRoundedStyle(radius: 12) instead of repeating the same setup throughout the codebase.
Layout Timing Matters
If Auto Layout controls the image view size, apply circle-style radii after the final bounds are known. Setting bounds.width / 2 too early usually works only by accident in static layouts. Putting the radius update in viewDidLayoutSubviews or a custom view layout pass makes the result predictable.
Common Pitfalls
- Setting
cornerRadiusbut forgettingclipsToBoundsorlayer.masksToBounds. - Calculating a circular radius before Auto Layout has given the image view its final size.
- Expecting shadows to remain visible on the same view that clips its contents.
- Applying corner radius to the wrong view in a nested hierarchy.
- Repeating the same layer styling logic everywhere instead of centralizing it.
Summary
- Set
layer.cornerRadiuson the image view's layer. - Enable clipping so the image content respects the rounded corners.
- For circles, compute the radius after layout using half the final width.
- Use a container view if you also need visible shadows.
- Centralize repeated image styling to keep UI code cleaner and more consistent.

