iOS Masonry介绍与使用

本文介绍了一种使用Masonry框架简化iOS应用UI布局的方法。Masonry通过提供简洁的链式语法来封装自动布局,使开发者能够轻松实现不同屏幕尺寸的适配。文中详细列举了Masonry支持的各种属性,并给出了具体的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于苹果公司不断推出新的机型,所以大家都知道iOS开发中UI布局常常需要适配,同时也应该了解到苹果设备的适配主要是4,5,6,6plus这4种设备的适配。适配的方法有代码适配和使用xib添加约束,其中代码适配是用到autolayout但是官方推出的文档和Demo实在是繁琐,个人觉得还不如笨办法使用宽高比来的实在(但是计算宽高比以及获取设备需要的方法和代码太多),直到发现Masonry这个做适配的第三方真是颇为简单好用,所以简单记录一下。

iPhone的尺寸规格

设备尺寸逻辑分辨率Scale Factor设备分辨率
3GS3.5inch320*480@1x320*480
4(s)3.5inch320*480@2x640*960
5(s)4inch320*568@2x640*1136
5c4inch320*568@2x640*1136
6(s)4.7inch375*667@2x750*1334
6(s)plus5.5inch414*736@3x1242*2208

Masonry的源码:https://github.com/SnapKit/Masonry

Masonry是一个轻量级的布局框架,它拥有自己的描述语法采用更优雅的链式语法封装自动布局简洁明了,具有高可读性

Masonry支持的属性有:

    //左侧,相当于NSAutoLayout的NSLayoutAttributeLeft
    @property (nonatomic, strong, readonly) MASConstraint *left;
    //上侧,相当于NSAutoLayout的NSLayoutAttributeTop
    @property (nonatomic, strong, readonly) MASConstraint *top;
    //右侧,相当于NSAutoLayout的NSLayoutAttributeRight
    @property (nonatomic, strong, readonly) MASConstraint *right;
    //下册,相当于NSAutoLayout的NSLayoutAttributeBottom
    @property (nonatomic, strong, readonly) MASConstraint *bottom;
    //首部,相当于NSAutoLayout的NSLayoutAttributeLeading
    @property (nonatomic, strong, readonly) MASConstraint *leading;
    //尾部,相当于NSAutoLayout的NSLayoutAttributeTrailing
    @property (nonatomic, strong, readonly) MASConstraint *trailing;
    //宽,相当于NSAutoLayout的NSLayoutAttributeWidth
    @property (nonatomic, strong, readonly) MASConstraint *width;
    //高,相当于NSAutoLayout的NSLayoutAttributeHeight
    @property (nonatomic, strong, readonly) MASConstraint *height;
    //横向中点(即x轴中点),相当于NSAutoLayout的NSLayoutAttributeCenterX
    @property (nonatomic, strong, readonly) MASConstraint *centerX;
    //纵向中点(即y轴中点),相当于NSAutoLayout的NSLayoutAttributeCenterY
    @property (nonatomic, strong, readonly) MASConstraint *centerY;
    //文本基线,相当于NSAutoLayout的NSLayoutAttributeBaseline
    @property (nonatomic, strong, readonly) MASConstraint *baseline;
了解了Masonry支持的属性后,我们大致也了解一下Masonry给我们提供的给控件添加约束的三个方法。

//    新增约束
    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
//    修改已有约束
    - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
//    清除之前已有约束,只保留新的约束
    - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

简单的事例:

1.居中显示一个宽度和高度都为200的UIView

代码:

__weak typeof (self) weakSelf = self;//防止block中循环引用
    UIView *centerView = [[UIView alloc] init];
    centerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:centerView];
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];

效果图:

2.相框效果.即子UIView小于supview。

代码:

__weak typeof (self) weakSelf = self;//防止block中循环引用
    UIView *photoView = [[UIView alloc] init];
    photoView.layer.borderWidth = 1;
    photoView.layer.borderColor = [UIColor blackColor].CGColor;
    photoView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:photoView];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"1"];
    [photoView addSubview:imageView];
    
    
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        方式一:
//        make.top.equalTo(photoView).offset(10);
//        make.left.equalTo(photoView).offset(10);
//        make.bottom.equalTo(photoView).offset(-10);
//        make.right.equalTo(photoView).offset(-10);
//        方式二:
//        make.top.left.bottom.right.equalTo(photoView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
//        方式三:
        make.edges.equalTo(photoView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
效果

使用Masonry的添加约束的之前,必须先把View添加到视图中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值