關(guān)于 requiresConstraintBasedLayout
裁奇,從文章中可以得知:
constraint-based layout engages lazily when someone tries to use it (e.g., adds a constraint to a view).
If you do all of your constraint set up in -updateConstraints, you might never even receive updateConstraints if no one makes a constraint.
To fix this chicken and egg problem,
override this method to return YES if your view needs the window to use constraint-based layout.
翻譯出來的意思就是基于約束的布局是懶觸發(fā)的围橡,也就是說只有在使用了約束的情況下少漆,系統(tǒng)才會(huì)自動(dòng)調(diào)用 - updateConstraints
方法章郁,如果我們?cè)趯懘a的時(shí)候,把所有約束放在 - updateConstraints
中碰纬,那么系統(tǒng)默認(rèn)是不知道你使用了約束進(jìn)行布局,這也就是一個(gè)雞生蛋還是蛋生雞的問題问芬,所以我們需要重寫 + requiresConstraintBasedLayout
返回 YES 悦析,告訴系統(tǒng)我們是基于約束進(jìn)行布局的,這樣就可以確保系統(tǒng)會(huì)回調(diào) -updateConstraints
方法此衅。
當(dāng)然如果你的布局不在- updateConstraints
里面强戴,或者部分在- updateConstraints
方法里面,則設(shè)置與否并沒有什么區(qū)別挡鞍。但是根據(jù)蘋果官方文檔的建議骑歹,我們還是盡量將頁面布局的代碼寫在-updateConstraints
里面,并且我們?cè)谥貙?code>-updateConstraints方法之后墨微,需要在代碼最后調(diào)用[super updateConstraints];
道媚。