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.
收到此消息后,將遍歷圖層的父圖層旨指,直到找到不需要布局的祖先圖層蒂破。 然后對該祖先圖層下面的整個層樹上所有視圖執(zhí)行布局荡短。
2.UIView call:
Overview
Lays out the subviews immediately, if layout updates are pending.
如果布局更新處于待處理狀態(tài)亿汞,則立即布置子視圖挠铲。
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.
使用此方法強制視圖立即更新其布局西雀。 使用“自動布局”時磺送,布局引擎會根據(jù)需要更新視圖的位置车份,以滿足約束的更改谋减。 使用接收此消息的視圖,如根視圖扫沼,此方法會布局整個子樹上的視圖出爹。 如果沒有待處理的布局更新,則此方法退出且不修改布局或調(diào)用任何與布局相關(guān)的回調(diào)缎除。
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.
此方法的默認實現(xiàn)在iOS 5.1及更早版本中不執(zhí)行任何操作严就。iOS 5.1之后,默認實現(xiàn)使用您設(shè)置的任何約束來確定任何子視圖的大小和位置器罐。
子類可以根據(jù)需要重寫此方法梢为,以對其子視圖執(zhí)行更精確的布局。 僅當(dāng)子視圖的自動調(diào)整大小和基本約束不能滿足你的需要時,才應(yīng)覆蓋此方法铸董。 您可以通過實現(xiàn)該方法直接設(shè)置子視圖的尺寸祟印。
您不應(yīng)該直接調(diào)用此方法。 如果要強制進行布局更新粟害,請在下次繪圖更新之前調(diào)用setNeedsLayout()
方法蕴忆。 如果要立即更新視圖的布局,請調(diào)用layoutIfNeeded()
方法悲幅。
setNeedsLayout()
1.CALayer call:
Overview
Invalidates the layer’s layout and marks it as needing an update.
使圖層的布局無效并將其標(biāo)記為需要更新套鹅。
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.
您可以調(diào)用此方法來指示圖層的子圖層的布局已更改并且必須更新。 當(dāng)圖層的邊界(bounds)發(fā)生變化或添加或刪除子圖層時夺艰,系統(tǒng)通常會自動調(diào)用此方法芋哭。 在macOS中,如果圖層的layoutManager屬性包含一個實現(xiàn)invalidateLayout(of :)
方法的對象郁副,那么也會調(diào)用該方法减牺。
在下一個更新周期中,系統(tǒng)調(diào)用任何需要布局更新的圖層的layoutSublayers()
方法存谎。
2.UIView call:
Overview
Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
使消息接收者的當(dāng)前布局無效并在下一個更新周期期間觸發(fā)布局更新拔疚。
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.
如果要調(diào)整視圖子視圖的布局,請在應(yīng)用程序的主線程上調(diào)用此方法既荚。 此方法記錄請求并立即返回稚失。 由于此方法不強制立即更新,而是等待下一個更新周期恰聘,因此可以在更新任何視圖之前使用它來使多個視圖的布局無效句各。 此行為允許您將所有布局更新合并到一個更新周期,這通常會提高性能晴叨。