xi.lin
xi.lin

Reputation: 3414

How to get the height constraint declared by Masonry for UIView?

For example, I declare some constraints through Masonry for a UIView:

[replyTextView mas_makeConstraints:^(MASConstraintMaker *make) {
  make.height.mas_equalTo(44);
  make.left.right.bottom.equalTo(self.contentView);
}];

How can I get the height constraint for the replyTextView?

I know I can use replyTextView.constraints to get all constraints, but I don't know how to tell them.

Upvotes: 0

Views: 4789

Answers (2)

vitalytimofeev
vitalytimofeev

Reputation: 291

You can do that this way:

   // in public/private interface
@property (nonatomic, strong) MASConstraint *heightConstraint;

...

self.heightConstraint = [[replyTextView mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.height.equalTo(@(44));
                            make.left.right.bottom.equalTo(self.contentView);
                        }] firstObject];//heightConstraint is first created MASConstraint
...

//heightConstraint is not composite MASConstraint, so we can access NSLayoutConstraint via KVC
NSLayoutConstraint *layoutConstraint = [heightConstraint valueForKey:@"layoutConstraint"];
CGFloat height = layoutConstraint.constant;//read
layoutConstraint.constant = height + 100;//modify

See also related discussion on the github page of Masonry: https://github.com/SnapKit/Masonry/issues/206

Upvotes: 3

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

as mentioned in the documentation:

   // in public/private interface
@property (nonatomic, strong) MASConstraint *heightConstraint;

...

// when making constraints
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.heightConstraint =  make.height.mas_equalTo(44);
}];

...
// then later you can call
[self.heightConstraint uninstall];

Upvotes: 4

Related Questions