Core Layout Runtime

In addition to the “display” pass Cocoa has always used, Cocoa Auto Layout adds two more passes that occur when rendering a window: updateConstraintsIfNeeded and layoutIfNeeded. These occur in order: update constraints, layout, display. If you invoke display manually, it invokes layout and layout invokes updateConstraints.

You can think of the update constraints pass as a measurement pass that communicates changes in geometry to the layout system. For example, if you change the title of a button at some point the text has to be measured, and a constraint has to be set up that communicates that information into the layout system. The update constraints pass is bottom-up.

It is in the layout pass that the frames of views are updated. The layout pass is top-down.

These passes have parallel sets of methods:

Display

Layout

Update Constraints

Override

Invoke Directly

Perform pass at window level

displayIfNeeded

(NSWindow)

layoutIfNeeded

(NSWindow)

updateConstraintsIfNeeded

(NSWindow)

No.

Normally called automatically by AppKit, but may be called if you need things to be up-to-date right now.

Perform pass at view level

Sets things up and invokes the view overridable method on all views in subtree that are dirty with respect to the pass.

displayIfNeeded

(NSView)

layoutSubtreeIfNeeded

(NSView)

updateConstraintsForSubtreeIfNeeded

(NSView)

No.

Normally called automatically by AppKit, but may be called if you need things to be up-to-date right now.

View level overridable method

Subclassers should override this method to do the requested work for the receiver. This implementation should not dirty the current or previous passes or invoke later passes.

drawRect:

(NSView)

layout

(NSView)

updateConstraints

(NSView)

Yes.

You should not call these methods directly.

Invalidation

Get / set methods for invalidation of the pass.

needsDisplay (NSView)

setNeedsDisplay: (NSView)

needsLayout (NSView)

setNeedsLayout: (NSView)

needsUpdateConstraints

(NSView)

setNeedsUpdateConstraints:

(NSView)

No.

Yes, if/when necessary.

If you’re implementing a custom view, the most common methods to interact with are the view-level overridable methods and the invalidation methods.

To reiterate: you never need to call setNeedsLayout: unless you override layout. Based on changes to constraints, the layout engine determines when layout is required.

A controller object is typically more concerned with the NSWindow and NSView methods that perform the pass. These uses are more rare, though still valid and needed on occasion.


Did this document help you? Yes It's good, but... Not helpful...