clipsToBounds
iOS development
Swift programming
UIView properties
app design

How does clipsToBounds work?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

clipsToBounds controls whether a view shows only the portion of its subviews that lies inside its own rectangular bounds. It sounds simple, but it affects shadows, corner rounding, scrolling effects, and many subtle layout bugs. The important thing to remember is that clipping is about visible rendering, not about frame geometry or hit testing alone.

What clipsToBounds Actually Does

clipsToBounds is a UIView property. When it is true, any subview content extending outside the parent view’s bounds is not drawn.

swift
1import UIKit
2
3let parent = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
4parent.backgroundColor = .lightGray
5parent.clipsToBounds = true
6
7let child = UIView(frame: CGRect(x: 80, y: 20, width: 80, height: 80))
8child.backgroundColor = .systemBlue
9
10parent.addSubview(child)

In this example, the blue child view extends beyond the right edge of the parent. With clipping enabled, only the part inside the 120 by 120 parent rectangle is visible.

Relationship to layer.masksToBounds

clipsToBounds is closely related to the underlying Core Animation property layer.masksToBounds.

In practice:

  • 'view.clipsToBounds = true maps to view.layer.masksToBounds = true'
  • 'view.clipsToBounds = false maps to view.layer.masksToBounds = false'

You usually set the UIView property unless you are working directly with layers.

swift
parent.clipsToBounds = true
print(parent.layer.masksToBounds) // true

Knowing this relationship helps when debugging interactions with layer-backed effects.

Common Use Cases

Clipping is useful when you want content strictly contained, such as:

  • image cropping inside a thumbnail container
  • horizontally scrolling cards masked to a fixed frame
  • rounded corners where overflowing subviews should be hidden

Example with an image view:

swift
1let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
2imageView.image = UIImage(named: "landscape")
3imageView.contentMode = .scaleAspectFill
4imageView.clipsToBounds = true
5imageView.layer.cornerRadius = 12

Without clipping, the image can visually extend beyond the rounded corners when using scaleAspectFill.

When Clipping Causes Problems

The most common surprise is shadows. If you clip a view that also draws a shadow, the shadow gets clipped away because the shadow extends outside the bounds.

swift
1let card = UIView(frame: CGRect(x: 20, y: 20, width: 200, height: 100))
2card.backgroundColor = .white
3card.layer.cornerRadius = 16
4card.layer.shadowOpacity = 0.2
5card.layer.shadowRadius = 8
6card.layer.shadowOffset = CGSize(width: 0, height: 4)
7card.clipsToBounds = true

With that configuration, the shadow is typically not visible. The common fix is wrapping:

  • outer view draws the shadow and does not clip
  • inner view clips content and applies corner radius

A Good Wrapper Pattern

Use two views when you need both rounded clipping and visible shadow.

swift
1let shadowContainer = UIView(frame: CGRect(x: 20, y: 20, width: 200, height: 100))
2shadowContainer.layer.shadowOpacity = 0.2
3shadowContainer.layer.shadowRadius = 8
4shadowContainer.layer.shadowOffset = CGSize(width: 0, height: 4)
5shadowContainer.clipsToBounds = false
6
7let contentView = UIView(frame: shadowContainer.bounds)
8contentView.backgroundColor = .white
9contentView.layer.cornerRadius = 16
10contentView.clipsToBounds = true
11
12shadowContainer.addSubview(contentView)

This pattern is standard in cards, sheets, and modern list cells.

Clipping and Scroll Views

UIScrollView and its subclasses use clipping behavior heavily. Visible content is constrained to the scroll view’s bounds while the content itself can extend beyond the current visible region. That is why you see only a window into the scroll content at any given moment.

If you disable clipping on a scroll container, overlapping visuals can appear outside the intended region and quickly become confusing.

Performance and Layout Notes

Clipping itself is not automatically bad, but it can interact with expensive rendering, especially if many layers are being animated or masked. Most apps do not need to optimize around clipsToBounds directly, but you should be aware that layering shadows, masks, rounded corners, and animations together can increase rendering cost.

Measure with Instruments if a heavily styled scrolling interface becomes sluggish.

Common Pitfalls

One common mistake is enabling clipsToBounds on a shadowed view and then wondering why the shadow disappears. Another is assuming clipping changes layout frames, when it only changes visible rendering. Developers also sometimes rely on clipping to “fix” overflowing layout instead of correcting the underlying constraints. Rounded corners with scaleAspectFill images are another frequent source of visual bugs when clipping is forgotten. Finally, wrapper views are often avoided at first, even though they are the cleanest solution for shadow-plus-corner designs.

Summary

  • 'clipsToBounds hides subview content that extends outside a view’s bounds.'
  • It effectively maps to the layer’s masksToBounds behavior.
  • Use it for contained content such as cropped images and rounded inner views.
  • Do not use it on the same view that must display an outside shadow.
  • Prefer a wrapper pattern when you need both clipping and shadow.
  • Remember that clipping affects rendering, not the actual layout geometry.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.