首页 > 解决方案 > iOS Masonry,如果另一个视图的 centerX 依赖于它,则视图的大小约束不起作用

问题描述

我在我的项目中使用 Masonry。首先,我对 rightToolBar 做了一些限制:

CGSize rightToolBarSize = CGSizeMake(54, 210);
[self.rightToolBar mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self).offset(-10);
    make.bottom.equalTo(self.mas_bottom).mas_offset(-20);
    make.size.mas_equalTo(rightToolBarSize);
}];

然后是另一个视图,头像:

[self.avatar mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(CGSizeMake(50, 50));
    make.bottom.equalTo(self.rightToolBar.mas_top).with.offset(-10);
    make.centerX.equalTo(self.rightToolBar.mas_centerX);
}];

这使得self.rightToolBar 的宽度变得比我预期的大 74 、 20 。如果我将头像的约束更改为:

[self.avatar mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(CGSizeMake(50, 50));
    make.bottom.equalTo(self.rightToolBar.mas_top).with.offset(-10);
    make.right.equalTo(self).with.offset(-17); // centerX -> right
}];

现在self.rightToolBar 的宽度是 54。但这没有任何意义,为什么设置头像的centerX会改变rightToolBar的宽度呢?

标签: iosmasonry

解决方案


也许您应该删除 rightToolBar 的约束并重新制作

[self.rightToolBar removeConstraints:self.constraints.constraints];
[self.rightToolBar mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self).offset(-10);
    make.bottom.equalTo(self.mas_bottom).mas_offset(-20);
    make.size.mas_equalTo(rightToolBarSize);
}];

推荐阅读