- Masonry布局定位約束沖突
視圖布局添加約束的時候,有的時候會報一對約束沖突:
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
"<MASLayoutConstraint:0x28244a340 UILabel:0x13b80b190.top == 。。。
有的時候,通過控制臺打印,能夠大致看出哪個子控件出現(xiàn)了問題,但是當(dāng)視圖控件和層級比較多時,就難以分辨。
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
這個時候我們可以通過添加斷點的方式,在查看定位約束沖突。步驟:

添加Symbolic Breakpoint

在symbol欄添加 UIViewAlertForUnsatisfiableConstraints
在Action欄 添加po [[UIWindow keyWindow] _autolayoutTrace]
此時重新運行工程,打開出現(xiàn)約束沖突的頁面,控制臺就會打印出視圖各層級以及地址,通過約束沖突的控件地址對比,就能找到相應(yīng)的視圖控件。

NSAutoresizingMaskLayoutConstraint 約束導(dǎo)致的約束沖突:self.translatesAutoresizingMaskIntoConstraints = NO;
- Masonry約束優(yōu)先級的設(shè)置
開發(fā)的時候,有的時候會遇到一行出現(xiàn)兩種文本展示框,約束添加不好,就會達(dá)不到想要的效果。

通過以上代碼,可以設(shè)置在同一行的文本框優(yōu)先展示哪一個文本框的內(nèi)容。然后可以根據(jù)現(xiàn)有項目的需要,來進(jìn)行設(shè)置。
效果如下:
