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.
收到此消息后,將遍歷圖層的父圖層旨指,直到找到不需要布局的祖先圖層蒂破。 然后對該祖先圖層下面的整個層樹上所有視圖執(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)用此方法既荚。 此方法記錄請求并立即返回稚失。 由于此方法不強制立即更新,而是等待下一個更新周期恰聘,因此可以在更新任何視圖之前使用它來使多個視圖的布局無效句各。 此行為允許您將所有布局更新合并到一個更新周期,這通常會提高性能晴叨。

Auto Layout 生命周期

Auto Layout.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末凿宾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子兼蕊,更是在濱河造成了極大的恐慌初厚,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件孙技,死亡現(xiàn)場離奇詭異产禾,居然都是意外死亡,警方通過查閱死者的電腦和手機牵啦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門亚情,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人哈雏,你說我怎么就攤上這事势似“柘模” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵履因,是天一觀的道長障簿。 經(jīng)常有香客問我,道長栅迄,這世上最難降的妖魔是什么站故? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮毅舆,結(jié)果婚禮上西篓,老公的妹妹穿的比我還像新娘。我一直安慰自己憋活,他們只是感情好岂津,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著悦即,像睡著了一般吮成。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上辜梳,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天粱甫,我揣著相機與錄音,去河邊找鬼作瞄。 笑死茶宵,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的宗挥。 我是一名探鬼主播乌庶,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼契耿!你這毒婦竟也來了瞒大?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宵喂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后会傲,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锅棕,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年淌山,在試婚紗的時候發(fā)現(xiàn)自己被綠了裸燎。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡泼疑,死狀恐怖德绿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤移稳,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布蕴纳,位于F島的核電站,受9級特大地震影響个粱,放射性物質(zhì)發(fā)生泄漏古毛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一都许、第九天 我趴在偏房一處隱蔽的房頂上張望稻薇。 院中可真熱鬧,春花似錦胶征、人聲如沸塞椎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽案狠。三九已至,卻和暖如春暇昂,著一層夾襖步出監(jiān)牢的瞬間莺戒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工急波, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留从铲,地道東北人。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓澄暮,卻偏偏與公主長得像名段,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泣懊,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355