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

标签 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.

最佳答案

NSDictionaryOfVariableBindings 的参数应该是 _otherView

它必须是与您在约束视觉格式中引用它的方式完全匹配的字符串。

关于ios - 以编程方式添加约束给我错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30178542/

相关文章:

ios - 当 x 轴变化时调整核心图中的 y 轴

javascript - 我需要帮助才能在 Xcode 中创建的简单 webview 应用程序中运行 Javascript Alert 和 Confirm

ios - 我向哪个 super View 添加约束重要吗?

ios - 以编程方式将 UiView 作为 subview

ios - AdMob 只发送真实广告,而不发送测试广告,为什么?

ios - 将值为 [NSNull null] 的字典转换为 JSON

ios - NEHotspotConfigurationManager 无法加入

ios - Objective-C 中的 BOOL 循环

ios - UITableView 异步 UIImage 设置

ios - 如何相对于父 View 的边距将 AddConstraint 添加到按钮,使其看起来距上边距 30 度,距右边距 30 度