About Layout
layoutIfNeeded():
1.CAlayer call:
Overview
Recalculate the receiver’s layout, if required.
如果需要的话重新计算消息接收者的布局
Discussion
When this message is received, the layer’s super layers are traversed until a ancestor layer is found that does not require layout. Then layout is performed on the entire layer-tree beneath that ancestor.
收到此消息后,将遍历图层的父图层,直到找到不需要布局的祖先图层。 然后对该祖先图层下面的整个层树上所有视图执行布局。
2.UIView call:
Overview
Lays out the subviews immediately, if layout updates are pending.
如果布局更新处于待处理状态,则立即布置子视图。
Discussion
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.
使用此方法强制视图立即更新其布局。 使用“自动布局”时,布局引擎会根据需要更新视图的位置,以满足约束的更改。 使用接收此消息的视图,如根视图,此方法会布局整个子树上的视图。 如果没有待处理的布局更新,则此方法退出且不修改布局或调用任何与布局相关的回调。
layoutSubviews()
Overview
Lays out subviews.
布局子视图
Discussion
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
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.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout()
method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded()
method.
此方法的默认实现在iOS 5.1及更早版本中不执行任何操作。iOS 5.1之后,默认实现使用您设置的任何约束来确定任何子视图的大小和位置。
子类可以根据需要重写此方法,以对其子视图执行更精确的布局。 仅当子视图的自动调整大小和基本约束不能满足你的需要时,才应覆盖此方法。 您可以通过实现该方法直接设置子视图的尺寸。
您不应该直接调用此方法。 如果要强制进行布局更新,请在下次绘图更新之前调用setNeedsLayout()
方法。 如果要立即更新视图的布局,请调用layoutIfNeeded()
方法。
setNeedsLayout()
1.CALayer call:
Overview
Invalidates the layer’s layout and marks it as needing an update.
使图层的布局无效并将其标记为需要更新。
Discussion
You can call this method to indicate that the layout of a layer’s sublayers has changed and must be updated. The system typically calls this method automatically when the layer’s bounds change or when sublayers are added or removed. In macOS, if your layer’s layoutManager property contains an object that implements the invalidateLayout(of:)
method, that method is called too.
During the next update cycle, the system calls the layoutSublayers()
method of any layers requiring layout updates.
您可以调用此方法来指示图层的子图层的布局已更改并且必须更新。 当图层的边界(bounds)发生变化或添加或删除子图层时,系统通常会自动调用此方法。 在macOS中,如果图层的layoutManager属性包含一个实现invalidateLayout(of :)
方法的对象,那么也会调用该方法。
在下一个更新周期中,系统调用任何需要布局更新的图层的layoutSublayers()
方法。
2.UIView call:
Overview
Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
使消息接收者的当前布局无效并在下一个更新周期期间触发布局更新。
Discussion
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.
如果要调整视图子视图的布局,请在应用程序的主线程上调用此方法。 此方法记录请求并立即返回。 由于此方法不强制立即更新,而是等待下一个更新周期,因此可以在更新任何视图之前使用它来使多个视图的布局无效。 此行为允许您将所有布局更新合并到一个更新周期,这通常会提高性能。