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);
        
    }];
Paste_Image.png

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

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));
    }];
}
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); 
}];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容