When is layoutSubviews called?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When developing applications in iOS, understanding the view layout process is crucial. One of the essential parts in this process is the layoutSubviews method in the UIView class. This method plays a vital role in adjusting the positions and sizes of views within a user interface. In this article, we'll delve into the technical aspects of layoutSubviews: when and why it is called, how it works, and best practices for using it.
Technical Explanation
layoutSubviews Method
In iOS development, layoutSubviews is a function that gets called whenever the frame of a view changes. It's defined in the UIView class and can be overridden in custom view subclasses to adjust the positioning and sizing of a view's subviews. The default implementation of this method does nothing, which means that if you need custom layout logic, you have to override it yourself.
When is layoutSubviews Called?
The layoutSubviews method is called during the layout pass of a view. Various actions or changes can trigger this layout pass:
- Frame Changes: When the
frameproperty of a view is changed explicitly, the system marks the view as needing layout and callslayoutSubviewson the view. - Adding or Removing Subviews: Whenever a subview is added or removed using methods like
addSubview(_:)orremoveFromSuperview(), the layout system may calllayoutSubviewsto adjust the layout. - Bounds Changes: If the
boundsof a view change – such as during animations or transformations –layoutSubviewsmay be called again to update the layout accordingly. - Calling
setNeedsLayout(): When you callsetNeedsLayout(), the system marks the view as needing layout, causinglayoutSubviewsto be triggered on the next layout pass. - Parent View Layout Change: If a parent view's layout changes, the system may then call
layoutSubviewson the child views.
Examples
Resizing a View
Imagine you have a custom button that needs to update its subviews' positions when resized. By overriding layoutSubviews, you can ensure your custom layout logic is executed whenever a resize occurs.
Responding to Parent Layout Changes
If you need a view to adjust its subviews when its parent view's bounds change (such as during an orientation change), override layoutSubviews.
Best Practices
- Call
super.layoutSubviews(): Always callsuper.layoutSubviews()in your overridden implementation to ensure that any auto-layout constraints are honored. - Avoid Expensive Operations: Do not perform resource-intensive operations in
layoutSubviewsas it can be called frequently and affect performance. - Minimize Hierarchy Changes: Frequent adding or removal of subviews can trigger multiple layout passes. Minimize these operations whenever possible.
- Use Auto Layout Wisely: Rely on Auto Layout constraints where possible to handle layout related tasks, as they can simplify your view layout code.
Summary Table
| Trigger Action | When layoutSubviews is Called |
| Frame Change | When view's frame changes |
| Adding/Removing Subviews | On adding or removing a subview |
| Bounds Change | When view's bounds changes |
Call setNeedsLayout() | On subsequent layout pass |
| Parent View Layout Change | When parent view's layout changes |
Additional Details
Optimizing Layout Passes
The UIKit layout engine is optimized to minimize the number of layout passes required. Here are tips to take advantage of this optimization:
- Batch Updates: Combine multiple changes to the views or constraints using methods such as
UIView.performWithoutAnimation(_:)to prevent unnecessary layout passes. - Avoid Unnecessary Calls to
setNeedsLayout(): CallingsetNeedsLayout()can lead to multiple recalculations. Use it judiciously.
Auto Layout Integration
Understanding where layoutSubviews fits within the Auto Layout cycle is vital. Auto Layout will trigger calls to layoutSubviews before executing constraints, so any manual layout logic should complement rather than contradict constraints.
In conclusion, understanding the intricacies of when and how layoutSubviews is called allows you to design efficient and responsive iOS user interfaces. By following best practices and leveraging Auto Layout capabilities, you can ensure that your views layout correctly under various conditions while maintaining performance.

