以编程方式添加约束会给我错误

时间:2015-05-11 22:16:38

标签: ios objective-c nslayoutconstraint

我尝试以编程方式向NSLayoutConstraint添加左右view。这是我的代码:

[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.

1 个答案:

答案 0 :(得分:1)

您对NSDictionaryOfVariableBindings的论证应为_otherView

它必须与您在约束可视格式中引用它的方式完全匹配。