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.

調(diào)用此方法可以調(diào)整子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.

調(diào)用此方法可以強(qiáng)制view立即更新約束。當(dāng)使用Auto Layout時(shí)候崭放,layout引擎會根據(jù)需要更新view的位置以滿足約束的變化紫新。所以一般都會和setNeedsLayout一起使用罐盔。如果希望立刻生成新的frame需要調(diào)用此方法揭厚,利用這點(diǎn)一般布局動(dòng)畫可以在更新布局后直接使用這個(gè)方法讓動(dòng)畫生效。
該方法執(zhí)行后會立刻調(diào)用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.

當(dāng)一個(gè)自定義的view的屬性影響到約束的時(shí)候筛圆,可以調(diào)用此方法告知需要更新約束,但是不會立刻更新椿浓。
它可以避免當(dāng)視圖約束多次改變時(shí)太援,每次都重新計(jì)算約束

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.

強(qiáng)制更新當(dāng)前view以及子view的約束。這個(gè)方法一般是系統(tǒng)來調(diào)用的扳碍,但是如果需要將約束更新到最新提岔,則可以手動(dòng)調(diào)用。
注意:該方法不應(yīng)該被重寫
該方法執(zhí)行后系統(tǒng)會調(diào)用updateConstraints笋敞。

layoutSubviews

系統(tǒng)重寫布局

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.

子類可以根據(jù)需要重寫此方法碱蒙,以更精確地布局其子視圖。

注意不要去手動(dòng)調(diào)用這個(gè)方法。

updateConstraints

系統(tǒng)更新約束

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.

一般情況下都不要手動(dòng)重寫這個(gè)方法赛惩,除非你要修改休書的地方執(zhí)行很慢哀墓,或者有冗余的約束。

注意:要在最后一步調(diào)用[super updateConstraints]

小結(jié)

stackoverflow上有個(gè)回答很全面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很少需要手動(dòng)調(diào)用

因此一般使用法則是:

  • 如果你直接操作約束喷兼,就使用setNeedsLayout
  • 如果你使用了setNeedsLayout,還想做一些其他的篮绰,比如說獲取到最新的frame,這時(shí)候就需要再setNeedsLayout追加一個(gè)layoutIfNeeded
  • 如果你重寫了updateConstraints方法季惯,這時(shí)候才需要調(diào)用setNeedsUpdateConstraints
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吠各,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子勉抓,更是在濱河造成了極大的恐慌贾漏,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琳状,死亡現(xiàn)場離奇詭異磕瓷,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)念逞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門困食,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人翎承,你說我怎么就攤上這事硕盹。” “怎么了叨咖?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵瘩例,是天一觀的道長。 經(jīng)常有香客問我甸各,道長垛贤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任趣倾,我火速辦了婚禮聘惦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘儒恋。我一直安慰自己善绎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布诫尽。 她就那樣靜靜地躺著禀酱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪牧嫉。 梳的紋絲不亂的頭發(fā)上剂跟,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼浩聋。 笑死观蜗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的衣洁。 我是一名探鬼主播墓捻,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坊夫!你這毒婦竟也來了砖第?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤环凿,失蹤者是張志新(化名)和其女友劉穎梧兼,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體智听,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡羽杰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了到推。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片考赛。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖莉测,靈堂內(nèi)的尸體忽然破棺而出颜骤,到底是詐尸還是另有隱情,我是刑警寧澤捣卤,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布忍抽,位于F島的核電站,受9級特大地震影響董朝,放射性物質(zhì)發(fā)生泄漏鸠项。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一子姜、第九天 我趴在偏房一處隱蔽的房頂上張望锈锤。 院中可真熱鬧,春花似錦闲询、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至记舆,卻和暖如春鸽捻,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工御蒲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留衣赶,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓厚满,卻偏偏與公主長得像府瞄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子碘箍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354