masonry使用技巧

2019-09-25  本文已影响0人  nadou23

1.当你初次设置约束时,使用mas_makeConstraints;当你界面的组件的当前约束会变大或者缩小时,就需要用mas_updateConstraints更新约束;当你的界面的组件的约束相对之前的参照物发生改变时,就需要用mas_remakeConstraints.(后面两种前提是你已经初次约束好)

2.当你要实现label宽度自适应时

[self addSubview:self.productLab];
[self.productLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self.productLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(InvestmentHeadTableViewCellSpace);
        make.left.equalTo(self).offset(16);
        //        make.right.equalTo(self.productValueLab.mas_left).offset(-5);
        //        make.width.equalTo(@(InvestDetailTableViewCellWidth)); 不要设置宽度,因为我需要宽度自适应
        make.height.equalTo(@25);
}];

3.根据官方demo,自定义view和各种子view ,告别之前需要使用- (instancetype)initWithFrame:(CGRect)frame,方法导致的又使用frame又使用masonry导致约束方法不统一的问题,自定义view为topView ,子view有titleLab,contentLab,tipImage

// 自定义view添加到父类视图
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(kHeadViewTopTitleHeight);
    }];
- (instancetype)init {
    self = [super init];
    if (self) {
    // 这里只加子view初始化代码
        [self initView];
    }
    return self;
}

// 需要开启autolayout
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

// 更新约束,苹果官方推荐
- (void)updateConstraints {
    UIView *superview = self;
    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.equalTo(superview.mas_top).offset(10);
//        make.width.mas_equalTo(200);
        make.height.mas_equalTo(25);
    }];
    
    [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.equalTo(self.titleLab.mas_bottom).offset(0);
//        make.width.mas_equalTo(130);
        make.height.mas_equalTo(21);
    }];
    
    [self.tipImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentLab.mas_right).offset(5);
        make.centerY.equalTo(self.contentLab);
        make.height.width.mas_equalTo(15);
    }];
    
    [super updateConstraints]; // 最后需要调super
}



- (void)initView {
    [self addSubview:self.titleLab];
    [self.titleLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];

    [self addSubview:self.contentLab];
    // 宽度自适应
    [self.contentLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];

    [self addSubview:self.tipImage];
    
}

// setter方法更新数据
- (void)setValue:(NSString *)value {
    _value = value;
    if (_value.length > 0) {
        self.titleLab.attributedText = str;
    // 告诉视图约束需要更新
        [self setNeedsUpdateConstraints];
   // 然后就可以利用动画平滑更新约束
        [self updateConstraintsIfNeeded];
   //   动画
        [UIView animateWithDuration:0.4 animations:^{
          // 调用这个马上进行重新布局
            [self layoutIfNeeded];
        }];
     }
}

使用masonry做动画:
直接看例子:

- (void)startAnimate {
// 通知视图布局需要更新
    [self setNeedsUpdateConstraints];
// 动画的使用还是用之前常用的动画方式
    [UIView animateWithDuration:0.3 animations:^{
     // 需要使用更新布局mas_updateConstraints
        [self.suu mas_updateConstraints:^(MASConstraintMaker *make) {
               make.height.mas_equalTo(100);
               make.width.mas_equalTo(100);
               make.top.left.equalTo(self).offset(0);
               make.bottom.right.equalTo(self).offset(0);
           }];
       // 通知立即更新布局
        [self.suu.superview layoutIfNeeded];
    }];
}

注意:使用masonry做自定义视图时,底座视图需要设置基本定位约束,而具体内容大小都可以由子视图整体撑起,这样就可以做到,不需要再暴露frame的初始化接口。
举例 Aview包含一个BsubView

 Aview *av = [[Aview alloc] init];
    av.backgroundColor = UIColor.greenColor;
    [self.view addSubview:av];
    [av mas_makeConstraints:^(MASConstraintMaker *make) {
       // 只需设置左,顶部位置约束
        make.left.equalTo(self.view).offset(100);
        make.top.equalTo(self.view).offset(100);
    }];

 BsubView *su = [[BsubView alloc] init];
    su.backgroundColor = UIColor.redColor;
    [self addSubview:su];
    [su mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(100);
        make.top.left.equalTo(self).offset(10);
        make.bottom.right.equalTo(self).offset(-10);
    }];

截屏2020-09-10 上午1.18.59.png
上一篇 下一篇

猜你喜欢

热点阅读