天天看点

iOS Masonry使用

#import “ViewController.h”

#import “Masonry.h”

@interface ViewController ()

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) UIView *backView1;

@property (nonatomic, strong) UILabel *oneLab;

@property (nonatomic, strong) UILabel *twoLab;

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    //在使用Masonry添加约束之前,需要在addSubview之后才能使用,否则会导致崩溃。

    //在添加约束时初学者经常会出现一些错误,约束出现问题的原因一般就是两种:约束冲突和缺少约束。对于这两种问题,可以通过调试和log排查。

    //之前使用Interface Builder添加约束,如果约束有错误直接就可以看出来,并且会以红色或者黄色警告体现出来。而Masonry则不会直观的体现出来,而是以运行过程中崩溃或者打印异常log体现,所以这也是手写代码进行AutoLayout的一个缺点。

    //mas_makeConstraints() 添加约束

    //mas_remakeConstraints() 移除之前的约束,重新添加新的约束

    //mas_updateConstraints() 更新约束

    //equalTo() 参数是对象类型,一般是视图对象或者mas_width这样的坐标系对象

    //mas_equalTo() 和上面功能相同,参数可以传递基础数据类型对象,可以理解为比上面的API更强大

    //width() 用来表示宽度,例如代表view的宽度

    //mas_width() 用来获取宽度的值。和上面的区别在于,一个代表某个坐标系对象,一个用来获取坐标系对象的值

    [self addSubChildViews];

    [self layoutSubViewsMotion];

    // Do any additional setup after loading the view, typically from a nib.

    }

  • (void)addSubChildViews {

    [self.view addSubview:self.backView];

    [self.backView addSubview:self.backView1];

    [self.backView1 addSubview:self.oneLab];

    [self.backView1 addSubview:self.twoLab];

    }

  • (void)layoutSubViewsMotion {

    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.mas_equalTo(20);

    make.top.mas_equalTo(20);

    make.right.mas_equalTo(-20);

    make.height.mas_equalTo(200);

    }];

    //一、内边距10===1.

    // [self.backView1 mas_makeConstraints:^(MASConstraintMaker *make) {

    // make.left.mas_equalTo(self.backView).mas_offset(10);

    // make.top.mas_equalTo(self.backView).mas_offset(10);

    // make.right.mas_equalTo(self.backView).mas_offset(-10);

    // make.bottom.mas_equalTo(self.backView).mas_offset(-10);

    // }];

    //========2.或者insets

    [self.backView1 mas_makeConstraints:^(MASConstraintMaker *make) {

    //下、右不需要写负号,insets方法中已经为我们做了取反的操作了

    make.edges.mas_equalTo(self.backView).insets(UIEdgeInsetsMake(10, 10, 10, 10));

    }];

    [_oneLab mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.top.mas_equalTo(10);

    make.right.mas_equalTo(-10);

    // make.height.mas_equalTo(self.backView1).mas_offset(0);//不设置按照文字适应

    }];

    [_twoLab mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.mas_equalTo(10);

    make.top.mas_equalTo(self.oneLab.mas_bottom).mas_offset(10);

    make.right.mas_equalTo(self.backView1).mas_offset(-10);

    }];

    //mas_update 更新布局

    [self.backView1 mas_updateConstraints:^(MASConstraintMaker *make) {

    make.bottom.mas_equalTo(self.twoLab);

    }];

    //mas_remake 重新布局

    // [self.backView1 mas_remakeConstraints:^(MASConstraintMaker *make) {

    // make.bottom.mas_equalTo(self.twoLab);

    // }];

    //二、设置约束优先级

    //

    // [self.twoLab mas_makeConstraints:^(MASConstraintMaker *make) {

    // make.center.equalTo(self.view);

    // make.width.equalTo(self.view).priorityLow();

    // make.width.mas_equalTo(20).priorityHigh();

    // make.height.equalTo(self.view).priority(200);

    // make.height.mas_equalTo(100).priority(1000);

    // }];

    //三、设置约束比列

    // 设置当前约束值乘以多少,例如这个例子是twoLab的宽度是backView1宽度的0.8倍。

    // [self.twoLab mas_makeConstraints:^(MASConstraintMaker *make) {

    // make.center.equalTo(self.backView1);

    // make.width.equalTo(self.backView1).multipliedBy(0.8);

    // }];

}

  • (UIView *)backView {

    if (_backView == nil) {

    _backView = [[UIView alloc] init];

    _backView.backgroundColor = [UIColor orangeColor];

    }

    return _backView;

    }

  • (UIView *)backView1 {

    if (_backView1 == nil) {

    _backView1 = [[UIView alloc] init];

    _backView1.backgroundColor = [UIColor brownColor];

    }

    return _backView1;

    }

  • (UILabel *)oneLab {

    if (_oneLab == nil) {

    _oneLab = [[UILabel alloc] init];

    _oneLab.backgroundColor = [UIColor cyanColor];

    _oneLab.font = [UIFont systemFontOfSize:13];

    _oneLab.textAlignment = NSTextAlignmentCenter;

    _oneLab.numberOfLines = 0;

    _oneLab.text = @“十五从军征,八十始得归。道逢乡里人:“家中有阿谁?” “遥看是君家,松柏冢累累。”(遥看 一作:遥望)兔从狗窦入,雉从梁上飞。中庭生旅谷,井上生旅葵。舂谷持作饭,采葵持作羹。羹饭一时熟,不知贻阿谁?出门东向看,泪落沾我衣。”;

    }

    return _oneLab;

    }

  • (UILabel *)twoLab {

    if (_twoLab == nil) {

    _twoLab = [[UILabel alloc] init];

    _twoLab.backgroundColor = [UIColor magentaColor];

    _twoLab.font = [UIFont systemFontOfSize:15];

    _twoLab.textAlignment = NSTextAlignmentLeft;

    _twoLab.numberOfLines = 0;

    _twoLab.text = @“涉江采芙蓉,兰泽多芳草。采之欲遗谁,所思在远道。还顾望旧乡,长路漫浩浩。同心而离居,忧伤以终老。”;

    }

    return _twoLab;

    }

over!