1

I am attempting to set my views top constraint (y-value) as a percentage (multiplier) of the superview's height. I need to use a reference rather than value constraint to account for the devices rotation.

The equivalent of what I am trying in non auto-layout is the following:

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

What I am trying to achieve is setting view's TOP constraint to 80% of superviews height like the following:

[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);
}];

However, this approach does not work. Is this possible with the Masonry library?

AamirR
  • 11,672
  • 4
  • 59
  • 73
elismm
  • 237
  • 3
  • 12
  • 1
    I'm not familiar with Masonry, but I have used constraints. Try: `make.top.equalTo(self.contentView.bottom).multipliedBy(0.8);` – vacawama May 15 '19 at 02:17

1 Answers1

1

Instead of contentView.mas_height use contentView.mas_bottom, try setting either labelView.top or labelView.bottom constraint, I think both should yield same result:

[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 equivalent

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)
}
AamirR
  • 11,672
  • 4
  • 59
  • 73