美文网首页
masonry使用技巧

masonry使用技巧

作者: nadou23 | 来源:发表于2019-09-25 23:36 被阅读0次

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

相关文章

  • masonry使用技巧

    一、基本用法 1、长度关系和位置关系,不能做比例运算。举几个例子我现在希望子视图的横向中心线(centerY)在高...

  • Masonry使用技巧

    一、相同View水平、竖直方向上排布 //- (void)mas_distributeViewsAlongAxis...

  • masonry使用技巧

    masonry git地址:https://github.com/SnapKit/Masonry 本文主要会讲到m...

  • masonry使用技巧

    1.当你初次设置约束时,使用mas_makeConstraints;当你界面的组件的当前约束会变大或者缩小时,就需...

  • Masonry 使用技巧

    Masonry 是一个轻量级的布局框架, 拥有自己的描述语法, 采用更优雅的链式语法封装自动布局 简洁明了 并具有...

  • Masonry

    Masonry使用方法Masonry的使用Masonry的github地址 本篇文章 Masonry的基本使用方法...

  • Masonry 单个约束的移除(卸载)和安装

    Masonry小技巧门来自 https://github.com/SnapKit/Masonry/issues/1...

  • 10.4 Masonry使用-动画

    Masonry使用-动画 会进行上下缩放 Masonry使用-动画1.png Masonry使用-动画2.png

  • Masonry使用方法

    Masonry的使用 Masonry的github地址 Masonry的基本使用方法 给控件添加约束使用Mason...

  • 第三方库--Masonry的基本使用

    Masonry是目前最流行的AutoLayout框架. 使用: 将Masonry文件包拖入项目 使用Masonry...

网友评论

      本文标题:masonry使用技巧

      本文链接:https://www.haomeiwen.com/subject/lutguctx.html