自动布局-Masonry

2017-08-28  本文已影响0人  RichieQ

一、简介

Masonry是NSLayoutConstraint的一种简易书写版本,没有更多的封装功能

二、系统AutoLayout更新布局的几个方法

三、使用

[self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.width.equalTo(self.topInnerView.mas_height).multipliedBy(3);
           
           make.width.and.height.lessThanOrEqualTo(self.topView);
           make.width.and.height.equalTo(self.topView).with.priorityLow();
           
           make.center.equalTo(self.topView);
       }];
// 创建视图约束
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      self.animatableConstraint = make.edges.equalTo(superview).insets(paddingInsets).priorityLow();
]];
// 更改约束 (另一处方法中)
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
self.animatableConstraint.insets = paddingInsets;
[self layoutIfNeeded]; //通过UIView animateWithDuration可以实现动画
/ 对某个view添加key值
greenView.mas_key = @"greenView";
// 或者如下顺序
MASAttachKeys(greenView, redView, blueView, superview);
// 同样的对每条约束亦可以添加key,key可以是任何对象
make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint2");
make.height.equalTo(redView.mas_height).key(@340954); //anything can be a key
- (void)layoutSubviews {
    [super layoutSubviews];

    //多行label设置preferredMaxLayoutWidth需要在[super layoutSubviews];之后
    self.longLabel.preferredMaxLayoutWidth = width;
    //preferredMaxLayoutWidth设置完后需要重新计算,因此需重新调用[super layoutSubviews];
    [super layoutSubviews];
}
    UIView* contentView = UIView.new;
    [self.scrollView addSubview:contentView];
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    UIView *lastView;
    CGFloat height = 25;
    
    for (int i = 0; i < 10; i++) {
        UIView *view = UIView.new;
        view.backgroundColor = [self randomColor];
        [contentView addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView ? lastView.bottom : @0);
            make.left.equalTo(@0);
            make.width.equalTo(contentView.width);
            make.height.equalTo(@(height));
        }];
        
        height += 25;
        lastView = view;
    }
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.bottom);
    }];
- (void)setupUI()
{
    [lowerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(10.0);
    }];

    [centerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
    }];

    [raiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).with.offset(-10);
    }];
    
    self.buttonViews = @[ raiseButton, lowerButton, centerButton ];
}

- (void)updateConstraints {
    [self.buttonViews updateConstraints:^(MASConstraintMaker *make) {
        make.baseline.equalTo(self.mas_centerY).with.offset(self.offset);
    }];
    
    //according to apple super should be called at end of method
    [super updateConstraints];
}
//相当于
//make.top.equalTo(superview).insets(padding);
//make.left.equalTo(superview).insets(padding);  
make.top.and.left.equalTo(superview).insets(padding);
  
//相当于
//make.left.equalTo(superview).insets(padding);
//make.right.equalTo(superview).insets(padding);  
//make.bottom.equalTo(superview).insets(padding); 
make.left.right.and.bottom.equalTo(superview).insets(padding);

make.width.and.height.lessThanOrEqualTo(self.topView);
/**
 *  多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值
 *
 *  @param axisType        轴线方向
 *  @param fixedSpacing    间隔大小
 *  @param leadSpacing     头部间隔
 *  @param tailSpacing     尾部间隔
 */
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType 
                    withFixedSpacing:(CGFloat)fixedSpacing 
                         leadSpacing:(CGFloat)leadSpacing 
                         tailSpacing:(CGFloat)tailSpacing;

/**
 *  多个固定大小的控件的等间隔排列,变化的是间隔的空隙
 *
 *  @param axisType        轴线方向
 *  @param fixedItemLength 每个控件的固定长度或者宽度值
 *  @param leadSpacing     头部间隔
 *  @param tailSpacing     尾部间隔
 */
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType 
                 withFixedItemLength:(CGFloat)fixedItemLength 
                         leadSpacing:(CGFloat)leadSpacing 
                         tailSpacing:(CGFloat)tailSpacing;
    [topView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];

四、源码

Github

上一篇 下一篇

猜你喜欢

热点阅读