如何获得Masonry为UIView声明的高度约束?

时间:2015-09-08 15:11:06

标签: ios objective-c uiview autolayout masonry

例如,我通过Masonry为UIView声明了一些约束:

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

如何获得replyTextView的高度限制?

我知道我可以使用replyTextView.constraints来解决所有限制,但我不知道如何告诉他们。

2 个答案:

答案 0 :(得分:4)

如文档中所述:

   // 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];

答案 1 :(得分:3)

你可以这样做:

   // 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

另见Masonry的github页面上的相关讨论:https://github.com/SnapKit/Masonry/issues/206