美文网首页
第三方库--Masonry的基本使用

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

作者: Hyman0819 | 来源:发表于2016-09-22 19:44 被阅读229次

    Masonry是目前最流行的AutoLayout框架.

    使用:

    1. 将Masonry文件包拖入项目
    2. 使用Masonry不需要设置
      控件的translatesAutoresizingMaskIntoConstraints属性为NO;
    3. equal_to ( 对象 ) 和 mas_equalTo( 常量 )

    Masonry简单使用


    相对父控件

     [imgv1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.top.equalTo(self).offset(20);
                make.size.mas_equalTo(CGSizeMake(100, 100));
            }];
    

    子控件之间

    位置、比例

    (控件的上下左右边都是对象)
     [imgv5 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(imgv1.mas_trailing).offset(10);
                make.top.equalTo(imgv1.mas_bottom).offset(10);
                make.width.equalTo(imgv1).multipliedBy(0.5);
                make.height.equalTo(imgv1).multipliedBy(0.5);
            }];
    

    位置、整体比例

    [imgv2 mas_makeConstraints:^(MASConstraintMaker *make) {
               make.right.equalTo(self).offset(-20);
               make.top.equalTo(self).offset(20);
               make.size.equalTo(imgv1).multipliedBy(0.5);
           }];
    

    内边距

    第一种:
    make.leading.top.mas_equalTo(@50);
    make.trailing.bottom.mas_equalTo(@-50);
    
    第二种:
    make.edges.insets(UIEdgeInsetsMake(50, 50, 50, 50));
    

    约束优先级

    [imgv1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.view);
            // 初始宽、高为100,优先级最低
            make.width.height.mas_equalTo(100 * self.scacle).priorityLow();
            // 最大放大到整个view
            make.width.height.lessThanOrEqualTo(self.view);
        }];
    

    注意事项:


    • 子视图只有被添加addsubviews( )之后,才能添加约束
    • 当更新约束时,你就需要在此调用此方法,因为 updateConstraints方法是需要触发的
    // 调用在view 内部,而不是viewcontroller
    +(BOOL)requiresConstraintBasedLayout
    {
        return YES;
    }
    /**
       *  苹果推荐 约束修改 放在此方法种
       */
    -(void)updateConstraints
    {
        [_imgv1 mas_updateConstraints:^(MASConstraintMaker *make) {
    
            make.leading.equalTo(self);
            make.top.equalTo(self).offset(20);
            make.size.mas_equalTo(CGSizeMake(100, 100));
        }];
        //最后添加super方法
        [super updateConstraints];
    }
    

    执行动画怎么办

    首先:子视图比例scale改变,导致变大
    self.scacle += 0.5;
    
    // 第一步(必须):self.view的设置需要更新的约束,但是不立即执行
    [self setNeedsUpdateConstraints];
    
    /**
     * 第二步(必须):通知self.view检测结果显示是否需要更新约束,
     * 若需要则立即更新,才会执行下面的动画
     */
    [self updateConstraintsIfNeeded];
    
    /**
     * 第三步(必须):self.view执行动画
     */
    [self animateWithDuration:0.4 animations:^{
       [self layoutIfNeeded];
    }];
    
    //第四步(必须):width和height都变大
    - (void)updateViewConstraints {
        [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.center.mas_equalTo(self.view);
            make.width.height.mas_equalTo(100 * self.scacle).priorityLow();// 初始宽、高为100,优先级最低
            make.width.height.lessThanOrEqualTo(self.view);// 最大放大到整个view
        }];
        [super updateViewConstraints];
    }
    

    重写约束mas_remake

        CGSize mysize = [item.title sizeWithFontSize:12 maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        
        if (mysize.width > kScreen_Width - 40) {
            
             [cell.titlelabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                 make.centerY.equalTo(cell.iconImageView);
                 make.centerX.equalTo(cell.iconImageView);
                 make.left.equalTo(cell.iconImageView).offset(10);
                 make.right.equalTo(cell.iconImageView).offset(-10);
             }];
    
        }else{
            [cell.titlelabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.iconImageView);
                make.centerX.equalTo(cell.iconImageView);
            }];
        }
    
    

    百度云盘Demo下载:执行动画更新约束

    相关文章

      网友评论

          本文标题:第三方库--Masonry的基本使用

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