【问题标题】:How to use Masonry mas_makeConstraints to avoid conflicts?如何使用 Masonry mas_makeConstraints 避免冲突?
【发布时间】:2016-07-04 08:19:51
【问题描述】:
[self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_line.superview).multipliedBy(0.5);
    }];

[self.line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_line.superview).multipliedBy(1.66666);
    }];

    "<MASLayoutConstraint:0x14efa7170 UIView:0x14ef3f5a0.centerY == UIView:0x14ef3ec00.centerY * 0.5>",
    "<MASLayoutConstraint:0x15026dcb0 UIView:0x14ef3f5a0.centerY == UIView:0x14ef3ec00.centerY * 1.66666>"

当我尝试更新约束时,会出现冲突,如何正确使用?

看起来更新操作创建了一个新的约束而不是改变它的值。

不能在mas_updateConstraints中应用吗?

我必须使用 mas_remakeConstraints 吗?

【问题讨论】:

  • 冲突描述是什么?
  • 看起来更新操作创建了一个新的约束,而不是改变它的值。
  • 根据 Masonry 所说,我认为您的代码没有问题。我研究了 Masonry 问题,作者 robertjpayne 说的however, you cannot alter the multiplier ( NSLayoutConstraint does not allow it ) so you need to use mas_remakeConstraints to update it.

标签: ios autolayout constraints masonry


【解决方案1】:

苹果文档说:

与其他属性不同,常量可以在约束创建后修改。在现有约束上设置常量比删除约束并添加一个与旧约束一样但具有新常量的新约束要好得多。

让我们在下面粘贴一些普通约束:

[NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top]

根据docmas_updateConstraints 应该改变现有约束的常量值,但是,乘数不是常量部分。

改用mas_remakeConstraints,根据doc,在安装新的之前删除所有现有的。

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多