如何使用 Masonry DSL 更新约束?

How to update constraints using Masonry DSL?

我在 updateConstraints:

中使用 Masonry 在代码中创建我的自动布局约束

我在控制台日志中得到以下信息:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7f8eabc684e0  Demo.View1:0x7f8eabc34680.top == Demo.View2:0x7f8eabc3ef20.bottom + 10>",
    "<MASLayoutConstraint:0x7f8eabc39420 Demo.View1:0x7f8eabc34680.top == Demo.View2:0x7f8eabc3ef20.bottom + 40>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x7f8eabc39420 Demo.View1:0x7f8eabc34680.top == Demo.View2:0x7f8eabc3ef20.bottom + 40>

为什么没有更新约束?

更新:

顺便说一句:我正在使用故事板。

此处的错误消息提供了很好的建议

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it.

并帮助打印出来

"<NSLayoutConstraint:0x7f8eabc684e0  Demo.View1:0x7f8eabc34680.top == Demo.View2:0x7f8eabc3ef20.bottom + 10>"
"<MASLayoutConstraint:0x7f8eabc39420 Demo.View1:0x7f8eabc34680.top == Demo.View2:0x7f8eabc3ef20.bottom + 40>"

注意两个约束和对象引用之间的相似性

view1 的顶部应该比 view2 的底部低 10 或 40。不能两者兼而有之吗?当您使用 Masonry 时,我怀疑它是您想要的 MASLayoutConstraint

找出 NSLayoutConstraint 的来源。它不是自动调整大小的,因为它们中有波浪线。所以要么你把它放在你的代码中,要么它来自 XIB/Storyboard。

如果是前者,您可以使用 Xcode 中的正则表达式查找来简化操作。我可能会试试这个。

通过搜索图标点击披露。您可以插入各种图案。

我可能会用这个作为开胃菜

您没有向我们展示您的约束条件,因此很难判断哪里出了问题。

我不确定为什么,但您不能使用 multipliedBy 等相对值更新约束。您可以做的是将约束更新为常量。以及您可以相对于屏幕尺寸计算的常数。像这样:

 _height_y = -self.view.frame.size.height*0.25;
[_phoneField updateConstraints:^(MASConstraintMaker *make) {
    make.baseline.equalTo(self.view.centerY).with.offset(_height_y);
}];