找不到属性的相关边

Nef*_*f10 4 ios autolayout nslayoutconstraint

我收到以下错误:

无法解析约束的符号常量,因为:无法找到属性的相关边:centerX 和 centerX。

在 NSLayoutConstraintFailedToFindDefaultResolvedValueForSymbolicConstant 处使用符号断点进行调试。

如果我添加一个断点,NSLayoutConstraintFailedToFindDefaultResolvedValueForSymbolicConstant它会停在这一行:

[self.customNavigationBar.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;

该行在viewDidLoad视图控制器内调用。customNavigationBar是一个从 nib 加载的 UIView,它已经作为子视图添加到self.view.

如果我尝试打印出我正在使用的锚点,一切似乎都可以:

(lldb) po self.customNavigationBar.widthAnchor
<NSLayoutDimension:0x17446cc80 "UIView:0x10115c160.width">

(lldb) po self.view.widthAnchor
<NSLayoutDimension:0x170667080 "UIView:0x1012ae550.width">
Run Code Online (Sandbox Code Playgroud)

Jon*_*gel 5

此错误来自您选择的NSLayoutConstraint.

你可能有这样的事情:

view.topAnchor.constraint(equalToSystemSpacingBelow: otherView.centerYAnchor, multiplier: 0.25).isActive = true
Run Code Online (Sandbox Code Playgroud)

但你应该像这样构建它:

let constraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: otherView, attribute: .centerY, multiplier: 0.25, constant: 1)

constraint.isActive = true
Run Code Online (Sandbox Code Playgroud)