NSGenericException', reason: 'Unable to install constraint on view

后端 未结 7 1542
花落未央
花落未央 2020-12-09 16:05

Terminating app due to uncaught exception \'NSGenericException\'

Terminating app due to uncaught exception \'NSGenericException\', reason: \'Unable to

7条回答
  •  不知归路
    2020-12-09 16:18

    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.

提交回复
热议问题