setNeedsLayout VS setNeedsUpdateConstraints

setNeedsLayout

Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.

调用此方法可以调整子view的布局,但是又不会立即更新布局。需要等到下次更新周期中才会更新。
此行为允许你将所有布局更新合并到一次更新周期中。

layoutIfNeeded

Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root. If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.

调用此方法可以强制view立即更新约束。当使用Auto Layout时候,layout引擎会根据需要更新view的位置以满足约束的变化。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
该方法执行后会立刻调用layoutSubviews。

setNeedsUpdateConstraints

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. Updating constraints all at once just before they are needed ensures that you don’t needlessly recalculate constraints when multiple changes are made to your view in between layout passes.

当一个自定义的view的属性影响到约束的时候,可以调用此方法告知需要更新约束,但是不会立刻更新。
它可以避免当视图约束多次改变时,每次都重新计算约束

updateConstraintsIfNeeded

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.

强制更新当前view以及子view的约束。这个方法一般是系统来调用的,但是如果需要将约束更新到最新,则可以手动调用。
注意:该方法不应该被重写
该方法执行后系统会调用updateConstraints。

layoutSubviews

系统重写布局

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

子类可以根据需要重写此方法,以更精确地布局其子视图。

注意不要去手动调用这个方法。

updateConstraints

系统更新约束

It is almost always cleaner and easier to update a constraint immediately after the affecting change has occurred. For example, if you want to change a constraint in response to a button tap, make that change directly in the button’s action method.
You should only override this method when changing constraints in place is too slow, or when a view is producing a number of redundant changes.

一般情况下都不要手动重写这个方法,除非你要修改休书的地方执行很慢,或者有冗余的约束。

注意:要在最后一步调用[super updateConstraints]

小结

stackoverflow上有个回答很全面setneedslayout-vs-setneedsupdateconstraints:

  • setNeedsUpdateConstraints makes sure a future call to updateConstraintsIfNeeded calls updateConstraints.
  • setNeedsLayout makes sure a future call to layoutIfNeeded calls layoutSubviews

When layoutSubviews is called, it also calls updateConstraintsIfNeeded, so calling it manually is rarely needed in my experience

作者的意思就是说updateConstraintsIfNeeded很少需要手动调用

因此一般使用法则是:

  • 如果你直接操作约束,就使用setNeedsLayout
  • 如果你使用了setNeedsLayout,还想做一些其他的,比如说获取到最新的frame,这时候就需要再setNeedsLayout追加一个layoutIfNeeded
  • 如果你重写了updateConstraints方法,这时候才需要调用setNeedsUpdateConstraints

你可能感兴趣的:(setNeedsLayout VS setNeedsUpdateConstraints)