天天看點

使用 UIViewAlertForUnsatisfiableConstraints 捕捉autolayout 限制沖突

很多時候我們會遇到限制沖突的問題、但是因為蘋果隻會在log中提示沖突、且這些沖突很難去了解、是以遇到沖突、也很難發現到底是哪裡出了問題、但是蘋果還是提供了方法、去debug的。

我們來看沖突的log:

-- :: Auto Layout Cookbook[:] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: () look at each constraint and try to figure out which you don't expect; () find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7b58bac0 'Label Leading' UILabel:0x7b58b040'Name'.leading == UIView:0x7b590790.leadingMargin>",
    "<NSLayoutConstraint:0x7b56d020 'Label Width' H:[UILabel:0x7b58b040'Name'(>=400)]>",
    "<NSLayoutConstraint:0x7b58baf0 'Space Between Controls' H:[UILabel:0x7b58b040'Name']-(NSSpace(8))-[UITextField:0x7b589490]>",
    "<NSLayoutConstraint:0x7b51cb10 'Text Field Trailing' UITextField:0x7b589490.trailing == UIView:0x7b590790.trailingMargin>",
    "<NSLayoutConstraint:0x7b0758c0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7b590790(320)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint: 'Label Width' H:[UILabel:'Name'(>=)]>

Make a symbolic breakpoint at **UIViewAlertForUnsatisfiableConstraints** to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
           

裡面寫了

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

這就意味着、我們可以通過symbolic breakpoint 來捕捉沖突、如何來設定呢?平時管理斷點的地方、點選加号、addMake a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.然後在symbol内容填上UIViewAlertForUnsatisfiableConstraints、如果出現沖突、系統會自動停留在斷點處、還是蠻好用的。

使用 UIViewAlertForUnsatisfiableConstraints 捕捉autolayout 限制沖突
使用 UIViewAlertForUnsatisfiableConstraints 捕捉autolayout 限制沖突

更多斷點知識、可以參考cocoa的文章隻會左鍵斷點?是時候試試這樣那樣斷點了