UIView in Swift — Part 3

Janvi Arora
6 min readMar 11, 2023

--

Triggering for AutoLayouts:

  1. func needsUpdateConstraints() -> Bool

A Boolean value that determines whether the view’s constraints need updating.

When this method returns true, the system calls the updateConstraints() method of the view, allowing it to make any necessary updates to its constraints. If needsUpdateConstraints() returns false, the updateConstraints() method is not called.

It’s important to note that the needsUpdateConstraints() method should be overridden in custom views that use Auto Layout, in order to ensure that the layout is correctly updated when necessary.

2. func setNeedsUpdateConstraints()

It is used to indicate that the view’s constraints need to be updated before the next layout pass. This method sets an internal flag that indicates the view’s constraints are inappropriate and need to be updated.

When this method is called, it doesn’t immediately trigger the layout system to update the constraints. Instead, it marks the view as needing constraint updates, and the actual constraint updates occur during the next layout pass.

This method is typically called when a change is made to the view that affects its constraints. For example, if a subview is added to or removed from the view, or if a property that affects layout, such as the view’s size or position, is changed.

It’s important to note that this method should not be called from within the updateConstraints() method or any other layout-related method. Doing so can lead to unexpected behavior in the layout system. Instead, it should be called from other parts of the code, such as in response to user input or data changes.

When a property of your custom view changes in a way that would impact constraints, you can call this method to indicate that the constraints need to be updated at some point in the future. The system will then call updateConstraints() as part of its normal layout pass. Use this as an optimization tool to batch constraint changes.

3. func updateConstraints()

Updates constraints for the view.

Override this method to optimize changes to your constraints.

To schedule a change, call setNeedsUpdateConstraints() on the view. The system then calls your implementation of updateConstraints() before the layout occurs. This lets you verify that all necessary constraints for your content are in place at a time when your custom view’s properties are not changing.

When you override the updateConstraints() method in a custom UIView subclass, you need to call the setNeedsUpdateConstraints() method to mark the view as needing an update. However, you should not call setNeedsUpdateConstraints() from inside your updateConstraints() implementation because doing so can lead to an infinite loop.

In addition, when you override the updateConstraints() method, you need to call [super updateConstraints()] as the final step in your implementation. This is important because the UIView class may need to update its own constraints, and failing to call super's implementation can result in unexpected behavior.

4. func updateConstraintsIfNeeded()

Updates the constraints for the receiving view and its subviews.

Whenever a new layout pass is triggered for a view, the system invokes this method to ensure that any constraints for the view and its subviews are updated with information from the current view hierarchy and its constraints. This method is called automatically by the system, but may be invoked manually if you need to examine the most up to date constraints.

Subclasses should not override this method.

Key differences:

needsUpdateConstraints() is used to check if the constraints of a view need to be updated, while setNeedsUpdateConstraints() is used to signal that the constraints of a view should be updated at some point in the future.

updateConstraints is automatically called by the system whenever the view needs to update its constraints, while updateConstraintsIfNeeded is called explicitly by your code.

Laying out subviews:

  1. func layoutSubviews()

layoutSubviews() is a method in UIView class that gets called whenever the layout of subviews needs to be updated. It is called automatically by the system whenever the bounds of the view changes or whenever the view is added to a superview.

This method is called after setNeedsLayout() is called on the view, indicating that the layout of the subviews of the view needs to be updated.

layoutSubviews() is responsible for updating the frames of the view's subviews. When a view's bounds changes, its subviews may also need to be resized or repositioned to fit the new bounds. This method is where that work happens.

It is important to note that layoutSubviews() is called every time the view is redrawn, so it should be implemented efficiently to avoid performance issues.

2. func setNeedsLayout()

Used to notify the system that the view needs to be laid out again.

When a view’s layout needs to be updated, calling setNeedsLayout() schedules a layout update for the next drawing cycle. During this update cycle, the system calls the view's layoutSubviews() method, which should be implemented by the view to update its subviews' layout and to adjust its own layout.

By calling setNeedsLayout() method, the system will automatically mark the view as needing layout, and at some point in the future, the system will trigger a call to the layoutSubviews() method of that view to update its layout.

It’s important to note that setNeedsLayout() does not immediately update the layout of the view. Instead, it sets a flag that tells the system to update the layout at the next drawing cycle.

This method is often used in combination with the layoutSubviews() method to create custom layouts for views, and can be called multiple times during the lifetime of a view to ensure that it is always up to date with any changes that may occur.

3. func layoutIfNeeded()

The layoutIfNeeded() method is used to force an immediate update to the layout of a view and its subviews, without waiting for the next layout cycle. If you have made changes to the layout constraints or geometry of a view, calling layoutIfNeeded() will immediately apply those changes and update the view's layout.

For example, suppose you have a view with a label subview that is centered horizontally and vertically within the parent view. If you change the text of the label, its intrinsic size may change, which in turn affects the layout of the parent view. To immediately update the layout with the new label size, you can call layoutIfNeeded() on the parent view.

It’s important to note that calling layoutIfNeeded() may result in additional layout passes and performance overhead, so it should be used judiciously. In general, you should avoid calling it during animations or other performance-sensitive operations, and instead let the normal layout cycle handle updates.

4. class var requiresConstraintBasedLayout: Bool { get }

The requiresConstraintBasedLayout class property is a boolean value that returns whether the view class requires constraints to perform its layout. If a view requires constraints for layout, its updateConstraints() method will be called during layout. If a view does not require constraints, its layoutSubviews() method will be called instead.

By default, most built-in views in UIKit, such as UIView, UILabel, and UIImageView, do not require constraint-based layout, and therefore their layoutSubviews() method is called during layout. However, custom views that require constraints for their layout can override the requiresConstraintBasedLayout property to return true, indicating that constraints are necessary for the view's layout.

5. var translatesAutoresizingMaskIntoConstraints: Bool

The translatesAutoresizingMaskIntoConstraints property is a Boolean value that determines whether the view’s autoresizing mask is translated into Auto Layout constraints.

If the value is set to true, the view's frame will be modified based on the autoresizing mask. If the value is set to false, the autoresizing mask will be translated into Auto Layout constraints, and the view's frame will be determined by those constraints.

When creating a view programmatically, its translatesAutoresizingMaskIntoConstraints property is set to true by default. However, when using Auto Layout to layout views, it is recommended to set this property to false to avoid conflicts between Auto Layout and autoresizing masks.

If you are using Interface Builder to layout views, this property can be set from the “Size inspector” tab in the Attributes inspector.

--

--

No responses yet