美文网首页
AutoLayout 使用(Masonry)总结

AutoLayout 使用(Masonry)总结

作者: wesk痕 | 来源:发表于2016-09-28 19:51 被阅读0次

    1 本来项目是使用绝对布局的,因为iOS9 SDK中IPhone对阿拉伯语全面的适配,而用户大部分也是使用阿拉伯语的,所以不得不适配,为了简单适配,那也就不得不使用AutoLayout。这里应该和大多数人一样使用的是第三方”Masonry”。https://github.com/SnapKit/Masonry

    @property (nonatomic, strong, readonly) MASConstraint *left; //居左
    @property (nonatomic, strong, readonly) MASConstraint *top; //居上
    @property (nonatomic, strong, readonly) MASConstraint *right; //居右
    @property (nonatomic, strong, readonly) MASConstraint *bottom; //居下
    @property (nonatomic, strong, readonly) MASConstraint *leading; 
    @property (nonatomic, strong, readonly) MASConstraint *trailing;
    @property (nonatomic, strong, readonly) MASConstraint *width;
    @property (nonatomic, strong, readonly) MASConstraint *height;
    @property (nonatomic, strong, readonly) MASConstraint *centerX;
    @property (nonatomic, strong, readonly) MASConstraint *centerY;
    

    例子:UITableViewCell 中的UIImageView的布局
    我们为了建立UIImageView的模型 有很多种方法 设置MASConstraintMaker的
    左上宽高 左右上下 中心点宽高 上centerX宽高 等等

    注意:在调用mas_makeConstraints 或 mas_updateConstraints 方法之前 先确保superView 存在,最好养成习惯在addSubview: 后布局使用AutoLayout

    在使用时superView 可以不用写 如下:

     [imageView mas_makeConstraints:^(MASConstraintMaker *make)
            {
                make.leading.equalTo(self.contentView).offset(16;
                make.top.equalTo(self.contentView).offset(0);
                make.trailing.equalTo(self.contentView).offset(-16);
                make.height.mas_equalTo(kCreateChatCellHeight);
    
            }];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make)
            {
                make.leading.mas_equalTo(16);
                make.top.mas_equalTo(0);
                make.trailing.mas_equalTo(-16);
                make.height.mas_equalTo(kCreateChatCellHeight);
    
            }];
    

    这两个代码的是等价的,所以从这也可以得出 superView是必须要存在的,要不然布局无法完成

    同一个方法中leading(trailing)和left(right)不能共存与同一个控件的约束
    如果考虑多语言环境,在布局时尽量使用leading trailing 而不是left right, 因为阿拉伯语布局是从右到左的(系统iOS9系统都已经开始支持了)。

    在使用mas_updateConstraints 更新时,属性需要保持一致。

    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make)
                 {
                     make.leading.equalTo(self.contentView).offset(Edge_Margin + Avar_Size + Icon_Margin + 12 +Label_Margin - 4);
                     make.top.equalTo(self.contentView).offset(19);
                     make.height.mas_equalTo(14);
                     make.trailing.equalTo(self.contentView).offset(-Label_Margin*2-Edge_Margin);
                 }];
    
    //数据改变需要刷新
    [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make)
                 {
                  //需要和前面定义过约束对应 不要采用make.bottom
                     make.top.equalTo(self.contentView).offset(30);
                 }];
    

    iOS6和iOS7 使用注意点
    implementation of -layoutSubviews needs to call super.
    在继承UIView 或 UILabel 等控件时 如果在layoutSubviews 方法有对应的布局 记得放在 [super layoutSubviews]; 方法之前

    - (void)layoutSubviews
    {
        // custom AutoLayout Code
        [label mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.leading.equalTo(imageView.mas_trailing).offset(16);
             ......
         }];
        [super layoutSubviews];
    
    }
    

    其实在UI布局是 尽量不要在layoutSubviews方法里写布局的代码,因为这个方法触发的地方有很多 如类的初始化、frame改变等等,相比于绝对布局,自动布局对性能有更大的影响。

    iOS6 crash :NSTextAlignmentJustified and NSTextAlignmentNatural are invalid alignment values when drawing an NSString
    注意xib中 UILabel textAlignment属性默认值是 NSTextAlignmentNatural
    需要手动设置

    对于从绝对布局到AutoLayout的转换能偷一个懒,我们在转换时建立一个UIView的分类

    - (void)setPBAutoLayout:(CGRect )frame
    {
        [self mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.leading.mas_equalTo(ceilf(frame.origin.x));
             make.top.mas_equalTo(ceilf(frame.origin.y));
             make.size.mas_equalTo(CGSizeMake(ceilf(frame.size.width), ceilf(frame.size.height)));
         }];
    }
    

    这是自己很久之前总结的文章了

    相关文章

      网友评论

          本文标题:AutoLayout 使用(Masonry)总结

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