Ios 以编程方式添加约束会导致错误

Ios 以编程方式添加约束会导致错误,ios,objective-c,nslayoutconstraint,Ios,Objective C,Nslayoutconstraint,我正试图以编程方式将左右NSLayoutConstraint添加到视图中。这是我的密码: [self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2.5-[_otherView]-2.5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.otherView)]]; 我运行应用程序时出现以下错误: Te

我正试图以编程方式将左右
NSLayoutConstraint
添加到
视图中。这是我的密码:

[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2.5-[_otherView]-2.5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.otherView)]];
我运行应用程序时出现以下错误:

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

_otherView is not a key in the views dictionary.

NSDictionaryOfVariableBindings
的参数应该是
\u otherView

它需要与您在约束视觉格式中引用它的方式完全匹配