Jessica
Jessica

Reputation: 9830

Adding constraint programmatically gives me error

I'm trying to add a left and right NSLayoutConstraint to a view programmatically. Here is my code:

[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2.5-[_otherView]-2.5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.otherView)]];

I get the following error when I run the app:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 

_otherView is not a key in the views dictionary.

Upvotes: 1

Views: 214

Answers (1)

dan
dan

Reputation: 9825

Your argument to NSDictionaryOfVariableBindings should be _otherView.

It needs to be an exact string match to the way you refer to it in the constraint visual format.

Upvotes: 1

Related Questions