You need to install the constraint on the "higher" of the two views. A good, general way to do this is like this:
NSLayoutConstraint* constraint = ...;
NSView* firstView = constraint.firstItem;
NSView* secondView = constraint.secondItem;
[[firstView ancestorSharedWithView: secondView] addConstraint: constraint];
Just a word of caution: It's good to remember here that constraint attributes are evaluated in the context of the view on which they are added. So for instance, the value of NSLayoutAttributeLeft of viewA, for a constraint installed on viewB, is interpreted in the coordinate space of viewB. For constraints that only reference sibling views or their superview, that fact is largely irrelevant, but there's no restriction that constraints can't reference two views that aren't siblings or direct parents.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…