码迷,mamicode.com
首页 > 其他好文 > 详细

Masonry 添加约束要注意顺序

时间:2016-06-24 14:29:28      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

对一个视图添加约束,其依赖的约束必须先已经存在,不能依赖该代码后的约束,否则造成不可预料的结果,如下代码能达到预期效果

- (void)makeConstraints {
    __weak typeof(self) weakSelf = self;

    [self.photoMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.photoButton).with.offset(-6);
        make.centerY.equalTo(weakSelf.photoButton);
        make.width.and.height.equalTo(@16);
    }];
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.photoButton);
        make.right.equalTo(weakSelf.photoMoreButton.mas_left);
        make.width.and.height.equalTo(@64);
    }];
}

技术分享

但是如果颠倒添加约束的顺序,如下

- (void)makeConstraints {
    __weak typeof(self) weakSelf = self;
 
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.photoButton);
        make.right.equalTo(weakSelf.photoMoreButton.mas_left);
        make.width.and.height.equalTo(@64);
    }];

     [self.photoMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.photoButton).with.offset(-6);
        make.centerY.equalTo(weakSelf.photoButton);
        make.width.and.height.equalTo(@16);
    }];
}

则得到的效果如下图

技术分享

添加约束时 photoImageView 依赖于 photoMoreButton,而当时 photoMoreButton 的约束还未设置,所以导致后面不正确的结果,使用Masonry时要注意这点!

Masonry 添加约束要注意顺序

标签:

原文地址:http://www.cnblogs.com/mforestlaw/p/5613913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!