DEV Community

Aleksei Barinov
Aleksei Barinov

Posted on

Bounds vs. Frame and can frame be less than bounds?

Hello everyone, this is a continuation of the longread series on interview questions for iOS developer positions. Today, we'll discuss one of the frequently asked questions — and that is: can bounds be less than frame and can frame be less than bounds? And another one what is the difference between bounds and frame?

This question often appears in interviews as a follow-up to "tell me about UIView," or as part of a broader discussion about view hierarchies and layout.

What Are Bounds and Frame?

Both bounds and frame are properties of UIView that define a view's size and position, yet they represent fundamentally different coordinate systems and purposes.

Frame defines a view's position and size relative to its superview. It's the window through which the outside world sees the view.

Bounds defines a view's position and size relative to itself. It's the internal coordinate system of the view's own content.

Consider a practical example:

This distinction becomes crystal clear when examining what happens during a rotation:

When Does Bounds Equal Frame? When Does It Differ?

When Bounds and Frame Are Identical

Bounds equals frame only when:

  • The view's origin is at (0, 0)
  • The view has no transforms applied (no rotation or scale)
  • The superview is the coordinate reference

When Bounds and Frame Differ Significantly

While bounds and frame may have identical values in simple cases, there are three critical scenarios where they diverge completely.

1. Transforms: Rotation and Scaling

When you apply transforms to a view, the frame expands to accommodate the transformed shape, while bounds remains unchanged because it represents the view's internal coordinate system.

What happens: The frame expands to the smallest axis-aligned rectangle that can contain the rotated view. This is why frame values change dramatically. Meanwhile, bounds preserves the view's logical dimensions—crucial for maintaining correct subview positioning.

2. Scrolling: The Bounds Origin Shift

UIScrollView demonstrates the most practical use of bounds.origin manipulation. When scrolling occurs, the frame stays fixed while bounds.origin shifts to reveal different content.

The magic: The scrollView's position in its superview never changes (frame stays at origin), but its bounds.origin shifts to (0, 200), effectively saying "start drawing my content from y=200 instead of y=0." This is the entire mechanism behind scrolling in iOS.

3. Position Changes in Superview

The simplest case: moving a view changes its frame but never affects its bounds, since the internal coordinate system remains independent.

Key insight: Any subviews positioned using bounds coordinates remain correctly placed because the internal coordinate system (bounds) is unaffected by external positioning (frame).

Why this knowledge will help you in your development, not just on interview.

Implementing Custom Scrolling

Any custom scrolling behavior requires manipulating bounds.origin. UIScrollView itself works by changing bounds.origin while keeping frame fixed.

Bug avoided: Many developers mistakenly try to implement scrolling by modifying frame, which causes the entire view to move in its superview instead of scrolling its content.

Layout Subviews Correctly

Bug avoided: Using frame instead of bounds for internal layout causes subviews to be positioned incorrectly, especially when the parent view has been transformed or positioned away from (0,0)

Handling Transforms

Bug avoided: Reading frame.size after applying transforms returns incorrect dimensions. Using bounds preserves accurate size information

Custom Drawing

Bug avoided: Using frame for drawing coordinates creates offset or incorrectly sized graphics, since frame uses the parent's coordinate system



That's it for this article! The bounds vs. frame distinction is fundamental to iOS development, and mastering it will set you apart in technical interviews.

Share in the comments what other questions about views, layout, or coordinate systems you were asked during interviews—your experience can help other candidates.

Make sure to subscribe to my Telegram channel so you don’t miss new articles and future updates.

See you soon in the next post!

Top comments (0)