-1

How do I find out who adds a specific auto layout constraint?

Because I'm seeing some constraints which I'm certain I did not add.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
  • Did you set `translatesAutoresizingMaskIntoConstraints` to `false`? – BallpointBen Mar 09 '17 at 16:58
  • Start using storyboards and xibs to define constraints. You will see that many such problems will go away. A constraint can be created from autoresizing mask for example. Sometimes a constraint can be added automatically when the current constraint are not suficient. – Sulthan Mar 09 '17 at 18:14
  • @BallpointBen, Yes. – Iulian Onofrei Mar 09 '17 at 18:24
  • @matt, It's a generic question. – Iulian Onofrei Mar 09 '17 at 18:25
  • @matt, "And the generic answer" is [this](http://stackoverflow.com/a/42807700/865175). Now please remove your downvote. – Iulian Onofrei Mar 15 '17 at 10:55
  • I'm not interested in your book. 1. A symbolic breakpoint would stop at __exactly__ where it was created so you'll see "who added it". 2. If you set the `accessibilityIdentifier` for your `UIView`s, you could distinguish them from others so you'll yet again know "who added" the constraints. You can also edit questions. Now please remove your downvote. – Iulian Onofrei Mar 15 '17 at 13:38
  • @matt, Am I right or not? – Iulian Onofrei Mar 17 '17 at 12:58

2 Answers2

1

In Swift you could look at var identifier: String?. If it starts with NS then it was added by iOS.

A constraint’s identifier is available in its description. Identifiers that start with NS are reserved by the system.
aahrens
  • 5,522
  • 7
  • 39
  • 63
0

If the layout is ambiguous or unsatisfiable, you can put a symbolic breakpoint on UIViewAlertForUnsatisfiableConstraints.

You can add a Debugger Command containing po [[UIWindow keyWindow] _autolayoutTrace] for Objective-C or expr -l objc++ -O -- [[UIWindow keyWindow] _autolayoutTrace] for Swift which automatically prints the UIView hierarchy, as desribed in this article.
You can also use po $r15 in the debugger to print the "details description of constraint that is broken" or po $r14 for "more detailed description of all constraints added in addition to breaking constraint", as described in this article.

If not, you can set the accessibilityIdentifier for your UIView (or directly in the IB) which will show up in the previous expressions' results (and other places too) instead of the string UIView.

Community
  • 1
  • 1
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113