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

时间:2022-01-23 20:16:28

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

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

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

1 个解决方案

#1


Your argument to NSDictionaryOfVariableBindings should be _otherView.

你对NSDictionaryOfVariableBindings的论证应该是_otherView。

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

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

#1


Your argument to NSDictionaryOfVariableBindings should be _otherView.

你对NSDictionaryOfVariableBindings的论证应该是_otherView。

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

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