【iOS自動約束】四個重要的系統(tǒng)方法

轉(zhuǎn)載地址:https://blog.csdn.net/gang544043963/article/details/52440621

用到自動約束吠架,就離不開一下四個方法:

@interface UIView (UIConstraintBasedLayoutCoreMethods)

- (void)updateConstraintsIfNeeded NS_AVAILABLE_IOS(6_0); // Updates the constraints from the bottom up for the view hierarchy rooted at the receiver. UIWindow's implementation creates a layout engine if necessary first.

- (void)updateConstraints NS_AVAILABLE_IOS(6_0); // Override this to adjust your special constraints during a constraints update pass

- (BOOL)needsUpdateConstraints NS_AVAILABLE_IOS(6_0);

- (void)setNeedsUpdateConstraints NS_AVAILABLE_IOS(6_0);

@end

而這四個方法衣式,對于它們的具體的用途和調(diào)用時機(jī)理解,很長一段時間我是迷迷糊糊的夫否。經(jīng)過一番研究,現(xiàn)在就試著把它解釋清楚7岣琛6杳薄!

- (void)updateConstraintsIfNeeded

官網(wǎng)文檔的解釋:

Updates the constraints for the receiving view and its subviews.

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.

更新視圖和它的子視圖的約束召噩。

每當(dāng)一個新的布局母赵,通過觸發(fā)一個視圖,系統(tǒng)調(diào)用此方法以確保視圖和其子視圖的任何約束與當(dāng)前視圖層次結(jié)構(gòu)和約束信息更新具滴。這種方法被系統(tǒng)自動調(diào)用凹嘲,但如果需要檢查最新的約束條件,可以手動調(diào)用這個方法构韵。

子類不應(yīng)重寫此方法周蹭。

注解:立即出發(fā)約束更新。 ?????

- (void)updateConstraints

官網(wǎng)文檔的解釋:

Updates constraints for the view.

Custom views that set up constraints themselves should do so by overriding this method. When your custom view notes that a change has been made to the view that invalidates one of its constraints, it should immediately remove that constraint, and then call setNeedsUpdateConstraints to note that constraints need to be updated. Before layout is performed, your implementation of updateConstraints will be invoked, allowing you to verify that all necessary constraints for your content are in place at a time when your custom view’s properties are not changing.

You must not invalidate any constraints as part of your constraint update phase. You also must not invoke a layout or drawing phase as part of constraint updating.

Important:Important

Call [super updateConstraints] as the final step in your implementation.

更新視圖的約束疲恢。

自定義視圖應(yīng)該通過重寫此方法來設(shè)置自己的約束凶朗。當(dāng)你的自定義視圖有某個約束發(fā)生了變化或失效了,應(yīng)該立即刪除這個約束显拳,然后調(diào)用setNeedsUpdateConstraints標(biāo)記約束需要更新棚愤。系統(tǒng)在進(jìn)行布局layout之前,會調(diào)用updateConstraints杂数,讓你確認(rèn)(設(shè)置)在視圖的屬性不變時的必要約束宛畦。在更新約束階段你不應(yīng)該使任何一個約束失效,而且不能讓layerout和drawing作為更新約束的一部分揍移。

重要提示:要在實現(xiàn)的最后調(diào)用[super updateConstraints]次和。

注解:自定義view應(yīng)該重寫此方法在其中建立constraints.

- (BOOL)needsUpdateConstraints

這個很簡單,返回是否需要更新約束羊精。constraint-based layout system使用此返回值去決定是否需要調(diào)用updateConstraints作為正常布局過程的一部分斯够。

- (void)setNeedsUpdateConstraints

官方文檔解釋:

Controls whether the view’s constraints need updating.

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)你的自定義視圖的屬性改變切影響到約束喧锦,你可以調(diào)用這個方法來標(biāo)記未來的某一點上需要更新的約束读规。然后系統(tǒng)將調(diào)用updateconstraints。

注解:這個方法和updateConstraintsIfNeeded關(guān)系有點曖昧燃少,updateConstraintsIfNeeded是立即更新束亏,二這個方法是標(biāo)記需要更新,然后系統(tǒng)決定更新時機(jī)阵具。

這里再補(bǔ)充一下Auto layout知識

Auto layout在view顯示之前碍遍,還有兩個步驟:updating constraints 和laying out views定铜。每一個步驟都依賴于上一個。display依賴layout怕敬,而layout依賴updating constraints揣炕。 updating constraints->layout->display

第一步:updating constraints愁拭,被稱為測量階段扎唾,其從下向上(from subview to super view),為下一步layout準(zhǔn)備信息倦春∫牛可以通過調(diào)用方法setNeedUpdateConstraints去觸發(fā)此步混聊。constraints的改變也會自動的觸發(fā)此步恨樟。但是粤咪,當(dāng)你自定義view的時候叹放,如果一些改變可能會影響到布局的時候斋日,通常需要自己去通知Auto layout牲览,updateConstraintsIfNeeded。

自定義view的話恶守,通车谙祝可以重寫updateConstraints方法,在其中可以添加view需要的局部的contraints熬的。

第二步:layout痊硕,其從上向下(from super view to subview),此步主要應(yīng)用上一步的信息去設(shè)置view的center和bounds押框〔沓瘢可以通過調(diào)用setNeedsLayout去觸發(fā)此步驟,此方法不會立即應(yīng)用layout橡伞。如果想要系統(tǒng)立即的更新layout盒揉,可以調(diào)用layoutIfNeeded。另外兑徘,自定義view可以重寫方法layoutSubViews來在layout的工程中得到更多的定制化效果刚盈。

第三步:display,此步時把view渲染到屏幕上挂脑,它與你是否使用Auto layout無關(guān)藕漱,其操作是從上向下(from super view to subview),通過調(diào)用setNeedsDisplay觸發(fā)崭闲,

因為每一步都依賴前一步肋联,因此一個display可能會觸發(fā)layout,當(dāng)有任何layout沒有被處理的時候刁俭,同理橄仍,layout可能會觸發(fā)updating constraints,當(dāng)constraint system更新改變的時候。

需要注意的是侮繁,這三步不是單向的虑粥,constraint-based layout是一個迭代的過程,layout過程中宪哩,可能去改變constraints娩贷,有一次觸發(fā)updating constraints,進(jìn)行一輪layout過程斋射。

注意:如果你每一次調(diào)用自定義layoutSubviews都會導(dǎo)致另一個布局傳遞育勺,那么你將會陷入一個無限循環(huán)中。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末罗岖,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子腹躁,更是在濱河造成了極大的恐慌桑包,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纺非,死亡現(xiàn)場離奇詭異哑了,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)烧颖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門弱左,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炕淮,你說我怎么就攤上這事拆火。” “怎么了涂圆?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵们镜,是天一觀的道長。 經(jīng)常有香客問我润歉,道長模狭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任踩衩,我火速辦了婚禮嚼鹉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘驱富。我一直安慰自己锚赤,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布萌朱。 她就那樣靜靜地躺著宴树,像睡著了一般。 火紅的嫁衣襯著肌膚如雪晶疼。 梳的紋絲不亂的頭發(fā)上酒贬,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天又憨,我揣著相機(jī)與錄音,去河邊找鬼锭吨。 笑死蠢莺,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的零如。 我是一名探鬼主播躏将,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼考蕾!你這毒婦竟也來了祸憋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤肖卧,失蹤者是張志新(化名)和其女友劉穎蚯窥,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塞帐,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡拦赠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了葵姥。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荷鼠。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖榔幸,靈堂內(nèi)的尸體忽然破棺而出允乐,到底是詐尸還是另有隱情,我是刑警寧澤牡辽,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布喳篇,位于F島的核電站,受9級特大地震影響态辛,放射性物質(zhì)發(fā)生泄漏麸澜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一奏黑、第九天 我趴在偏房一處隱蔽的房頂上張望炊邦。 院中可真熱鬧,春花似錦熟史、人聲如沸馁害。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碘菜。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間忍啸,已是汗流浹背仰坦。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留计雌,地道東北人悄晃。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像凿滤,于是被迫代替她去往敵國和親妈橄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容