Masonry小结

multipliedBy(约束值为约束对象的百分比)用法:

   //button的宽度,占屏幕宽度的一半
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
        make.height.equalTo(@100);
        
    }];
Masonry小结_第1张图片
Paste_Image.png

动态布局,根据内容的大小,父视图大小根据子视图大小改变

Masonry小结_第2张图片
Paste_Image.png
-(void)testLabelNumberofLines{
    
    UIView*bgView0 = [[UIView alloc]init];
    bgView0.backgroundColor = [UIColor redColor];
    [self.view addSubview:bgView0];
    [bgView0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
    }];
    
    
    UIView*bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor yellowColor];
    [bgView0 addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView0);
        make.top.equalTo(bgView0).offset(20);
        make.bottom.equalTo(bgView0).offset(-20);
    }];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"共勉吧,在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。";
    [bgView addSubview:label];
    label.backgroundColor = [UIColor grayColor];
    label.numberOfLines = 0;
    [label sizeToFit];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(bgView.mas_top).offset(20);
        make.bottom.equalTo(bgView).offset(-20);
    }];
    
}

//相对于父视图边距为10简洁写法
UIEdgeInsets使用

-(void)testUIEdgeInsets{
    UIView *view0 = [UIView new];
    view0.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view0];
    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100);
        make.centerX.equalTo(self.view);
        make.height.equalTo(@100);
        make.width.equalTo(@100);
    }];
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = [UIColor redColor];
    [view0 addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view0).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
}
Masonry小结_第3张图片
Paste_Image.png
// priority的使用 
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) { 
make.left.greaterThanOrEqualTo(self.view.mas_left).with.priorityLow(); 
make.top.equalTo(self.view.mas_top).with.priority(600); 
}];

你可能感兴趣的:(Masonry小结)