elismm
elismm

Reputation: 257

Setting masonry x/y constraints as a percentage of the views width/height

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?

Upvotes: 1

Views: 1150

Answers (1)

AamirR
AamirR

Reputation: 12208

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)
}

Upvotes: 1

Related Questions