【问题标题】:Setting masonry x/y constraints as a percentage of the views width/height将砌体 x/y 约束设置为视图宽度/高度的百分比
【发布时间】:2019-10-01 22:46:23
【问题描述】:

我正在尝试将我的视图顶部约束(y 值)设置为超级视图高度的百分比(乘数)。我需要使用参考而不是值约束来解释设备旋转。

我在非自动布局中尝试的等价物如下:

[self.labelView setFrame:CGRectMake(0, self.frame.size.height * 0.8, self.frame.size.width, 20)];

我想要实现的是将视图的 TOP 约束设置为超级视图高度的 80%,如下所示:

[self.labelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView.mas_height).multipliedBy(0.8);
    make.left.and.right.equalTo(self.contentView);
    make.height.equalTo(20);
}];

但是,这种方法行不通。 Masonry 库可以做到这一点吗?

【问题讨论】:

  • 我对 Masonry 不熟悉,但我使用了约束。试试看:make.top.equalTo(self.contentView.bottom).multipliedBy(0.8);

标签: ios objective-c swift autolayout masonry-ios-osx


【解决方案1】:

不要使用contentView.mas_height 使用contentView.mas_bottom,尝试设置labelView.toplabelView.bottom 约束,我认为两者应该产生相同的结果:

[labelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.contentView.mas_bottom).multipliedBy(0.8);
    make.left.and.right.equalTo(self.contentView);
    make.height.equalTo(@20);
}];

Swift 等价物

labelView.mas_makeConstraints { (make:MASConstraintMaker!) in
    make.bottom.equalTo()(self.contentView.mas_bottom)!.multipliedBy()(0.8)
    make.left.and().right().mas_equalTo()(self.contentView)
    make.height.mas_equalTo()(20.0)
}

【讨论】:

  • obj-c 代码正是我想要的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 2017-10-14
  • 2010-10-17
  • 2012-04-05
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多