AppsDev
AppsDev

Reputation: 12499

How to identify views in Debug area when unsatisfiable constraints?

I am using Autolayout and setting constraints programmatically, and I am getting some errors of unsatisfiable constraints when I run the app, for instance:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x798cd1c0 H:[UIImageView:0x78e03c20(40)]>

I have several UIImageView like this and I am finding hard to know which is making that constraint to be broken. Is there anyway to set, lets say, name tags to views to better identify them in the debug area?

Upvotes: 2

Views: 1174

Answers (1)

Bharat Modi
Bharat Modi

Reputation: 4180

Xcode's Auto layout has provided a function to set the identifier to the constraints. Using this feature it is very easy to get the find which constraint is not necessary.

To set identifier to constraint just select the constraint on the storyboard, and you would be prompt with the pop up of selected constraint properties as below in the size inspector,

enter image description here

For demonstration purpose, i have designed one screen having three imageViews vertically aligned as below,

enter image description here

I Have set Fixed width of 240 pts to each imageView and LeadingSpace and TrailingSpace to 40 pts, so this would create conflict in between these two constraint. See the log of breaking constraint before setting the identifier to constraint,

"NSLayoutConstraint:0x7fbce9cb9180 H:[UIImageView:0x7fbce9cbb8c0(240)]>",
"<NSLayoutConstraint:0x7fbce9cbe450 H:[UIImageView:0x7fbce9cbb8c0]-(40)-|   (Names: '|':UIView:0x7fbce9cb6320 )",

"NSLayoutConstraint:0x7fbce9cbe4a0 H:|-(40)-[UIImageView:0x7fbce9cbb8c0]   (Names: '|':UIView:0x7fbce9cb6320 )",

"NSLayoutConstraint:0x7fbce9c29b30 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fbce9cb6320(375)]"

After setting identifier,

"NSLayoutConstraint:0x7ff7da44ba70
H:[UIImageView:0x7ff7da448ba0]-(40)-|   (Names:
'|':UIView:0x7ff7da448750 )",

"NSLayoutConstraint:0x7ff7da44bac0 H:|-(40)-[UIImageView:0x7ff7da448ba0]   (Names:
 '|':UIView:0x7ff7da448750 )",

"NSLayoutConstraint:0x7ff7da447d30 'RedImageViewWidth' H:[UIImageView:0x7ff7da448ba0(240)]",  

"NSLayoutConstraint:0x7ff7da4206a0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff7da448750(375)]"

The trick in reading the log of constraint break is reading the numbers, in the above you can see the numbers as '240', '40' and '375', and just try to find the constraint having the same constant values on storyboard,

In my case the log messages indicates that, i have Leading and Trailing space of 40 pts to SuperView and at the same time has Width of 240 pts, so auto layout is breaking this width constraint and setting the width of 375 pts as per the screen size by having the Leading and Trailing space of 40 pts.

Hope this would help you.

Upvotes: 1

Related Questions