是否可以通过iOS中另一个视图的大小来抵消约束?

时间:2021-01-14 20:27:33

I am using masonry to programmatically setup constraints on views. Several times I have wanted to offset a constraint by a characteristic of another view. Is that possible?

我正在使用masonry以编程方式设置视图约束。有几次我想通过另一个视图的特征来抵消约束。那可能吗?

For example, I'd like to do this:

例如,我想这样做:

[self.someView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.mas_centerX).offset(self.otherView.mas_width);
}];

which of course doesn't compile because offset expects a number and not a constraint.

这当然不能编译,因为offset需要一个数字而不是一个约束。

1 个解决方案

#1


0  

Instead of self.otherView.mas_width in your offset, why not use self.otherView.frame.size.width? Use height instead of width if you want the height of the view. This is the correct way to get the width or height of a view.

而不是偏移中的self.otherView.mas_width,为什么不使用self.otherView.frame.size.width?如果想要视图的高度,请使用height而不是width。这是获取视图宽度或高度的正确方法。

#1


0  

Instead of self.otherView.mas_width in your offset, why not use self.otherView.frame.size.width? Use height instead of width if you want the height of the view. This is the correct way to get the width or height of a view.

而不是偏移中的self.otherView.mas_width,为什么不使用self.otherView.frame.size.width?如果想要视图的高度,请使用height而不是width。这是获取视图宽度或高度的正确方法。