|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Autolayout breakpoints" |
| 4 | +author: rafa |
| 5 | +date: 2015-08-17 20:00:52 +0200 |
| 6 | +comments: false |
| 7 | +categories: |
| 8 | +--- |
| 9 | + |
| 10 | +Auto layout has become a crucial tool for iOS and OS X development. It makes creating layout for multiple screen sizes easy peasy. But sometimes it can drive you crazy, with verbose and misleading logs. |
| 11 | + |
| 12 | +``` |
| 13 | +Unable to simultaneously satisfy constraints. |
| 14 | +Probably at least one of the constraints in the following list is one you don't want. |
| 15 | +Try this: |
| 16 | +
|
| 17 | +(1) look at each constraint and try to figure out which you don't expect; |
| 18 | +(2) find the code that added the unwanted constraint or constraints and fix it. |
| 19 | +(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) |
| 20 | +
|
| 21 | +(...........) |
| 22 | +
|
| 23 | +
|
| 24 | +Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. |
| 25 | +The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. |
| 26 | +``` |
| 27 | + |
| 28 | +That's a huge log! And I cut off the `NSLayoutConstraint` part. Yet, the second last line is giving a clue in which direction to go to fix this issue. Symbolic breakpoint at `UIViewAlertForUnsatisfiableConstraints`. |
| 29 | + |
| 30 | +All right, here's what Xcode want's you to do: |
| 31 | + |
| 32 | +{% img center /images/autolayout-breakpoints/1.png %} |
| 33 | + |
| 34 | +Honestly, that won't help much, because basically it'll just stop the execution and leave you up with `LLDB`, alone in the dark. |
| 35 | + |
| 36 | +But there's a little trick you can do to enhance the preceding symbolic breakpoint. |
| 37 | +Adding `po [[UIWindow keyWindow] _autolayoutTrace]` to it. |
| 38 | + |
| 39 | +{% img center /images/autolayout-breakpoints/2.png %} |
| 40 | + |
| 41 | +Now, on console, you'll see all the `UIView` hierarchy and exactly where it has ambiguity. |
| 42 | + |
| 43 | +```objc |
| 44 | +UIWindow:0x7f9481c93360 |
| 45 | +| •UIView:0x7f9481c9d680 |
| 46 | +| | *UIView:0x7f9481c9d990- AMBIGUOUS LAYOUT for UIView:0x7f9481c9d990.minX{id: 13}, UIView:0x7f9481c9d990.minY{id: 16} |
| 47 | +| | *_UILayoutGuide:0x7f9481c9e160- AMBIGUOUS LAYOUT for _UILayoutGuide:0x7f9481c9e160.minY{id: 17} |
| 48 | +| | *_UILayoutGuide:0x7f9481c9ebb0- AMBIGUOUS LAYOUT for _UILayoutGuide:0x7f9481c9ebb0.minY{id: 27} |
| 49 | +``` |
| 50 | +
|
| 51 | +Note that as you hit continue it'll stop at every ambiguous layout you may have. |
| 52 | +And if that's not enough for you to find out your autolayout issue, try changing the view's color, who knows? |
| 53 | +
|
| 54 | +```objc |
| 55 | +(lldb) expr ((UIView *)0x7f9ea3d43410).backgroundColor = [UIColor redColor] |
| 56 | +(UICachedDeviceRGBColor *) $1 = 0x00007f9ea3d43410 |
| 57 | +``` |
| 58 | + |
| 59 | +Fear no more young Padawan, make symbolic breakpoints and `LLDB` work for you! |
0 commit comments