iOS开发 Masonry的简单使用

摘要:
只读)MASConstraint*right;只读)MASConstraint*尾随;只读)MASConstraint*高度;只读)MASConstraint*centerY;只读)MASConstraint*基线;(void(^)(MASConstraintMaker*make))块;

首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout

iOS开发 Masonry的简单使用第1张
 
iOS开发 Masonry的简单使用第2张
 

从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需求。这里就不详细讲解具体拖拽的方法.....

然后,我们按着上图的属性来看看如何简单得使用Masonry

这里是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;  //纵向居中

@property (nonatomic, strong, readonly) MASConstraint *baseline; //文本基线

属性有了,接着我们应该怎么在视图中添加约束呢,Masonry给我们提供了3个方法

//新增约束
 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

//更新约束
 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

//清楚之前的所有约束,只会保留最新的约束
 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
 
 合理的利用这个3个函数,基本上可以应对任何情况了

准备工作已经完成,我们来看几个小demo

1.居中一个view

    // 防止block中的循环引用
    __weak typeof (self) weakSelf = self;
    // 初始化一个View
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor redColor];
    [self.view addSubview:bgView];
    // 使用mas_makeConstraints添加约束
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];

iOS开发 Masonry的简单使用第3张
效果图1

是不是很简单,这里有一点要必须注意下,添加约束前必须要把view添加到视图上。

那我要是不想固定他得宽高呢,让view的大小根据间距来控制怎么做

我们来设置一个基于父视图间距为10的view

[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));
 }];

这样就ok了!!!

make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));

等同于 

    make.top.equalTo(weakSelf.view).with.offset(10);
    make.left.equalTo(weakSelf.view).with.offset(10);
    make.bottom.equalTo(weakSelf.view).with.offset(-10);
    make.right.equalTo(weakSelf.view).with.offset(-10);

2.多个view

2个view横向居中,第二个view距离第一个view间距为10

    UIView *view1 = [[UIButton alloc]init];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(90, 90));
        make.centerX.equalTo(weakSelf.view);
        make.top.width.offset(90);
    }];
   
    UIView *view2 = [[UILabel alloc]init];
    view2.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.centerX.equalTo(view1);
        make.top.equalTo(view1.mas_bottom).with.offset(20);
    }];

iOS开发 Masonry的简单使用第4张
效果图2

大家有没有看到第二个view代码中

make.top.equalTo(view1.mas_bottom).with.offset(20);

view1.mas_bottom 是什么意思呢?如果只写view1,Masonry会默认是view1中最上面开始算起,也就是view2 间距view1 Y轴开始20的间距

通过这个也就可以很方便的设置view同另一个view之间上下左右的间距了

大家不妨试试view.mas_top  view.mas_left  view.mas_right 的效果是什么样得了

下面我附上一个完整的界面demo,大家可以看看

iOS开发 Masonry的简单使用第5张
效果图3

代码如下:

- (void)setupFrame {
    __weak typeof(self) weakSelf = self;

    //上传头像
    UIButton *iconBtn = [[UIButton alloc]init];
    [iconBtn setCornerRadius:45];
    [iconBtn setBackgroundImage:[UIImage imageNamed:@"huantouxiang"] forState:UIControlStateNormal];
    [iconBtn addTarget:self action:@selector(iconButton) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:iconBtn];
    self.iconBtn = iconBtn;

    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(90, 90));
        make.centerX.equalTo(weakSelf.view);
        make.top.width.offset(90);
    }];
   
    //上传社区头像文字提醒
    UILabel *iconLabel = [[UILabel alloc]init];
    iconLabel.textColor = c3;
    iconLabel.text = @"上传社团头像";
    iconLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:iconLabel];
   
    [iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconBtn);
        make.top.equalTo(iconBtn.mas_bottom).with.offset(20);
    }];
   
    //社团编辑图标
    UIImageView *editIcon = [[UIImageView alloc]init];
    editIcon.image = [UIImage imageNamed:@"bianxie"];
    [self.view addSubview:editIcon];
   
    [editIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 20));
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.top.equalTo(iconLabel.mas_bottom).with.offset(30);
    }];
   
    //社团名
    UITextField *nameText = [[UITextField alloc]init];
    nameText.placeholder = @"请填写社区名(社团名最多6个字)";
    [self.view addSubview:nameText];
    self.nameText = nameText;
   
    [nameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@20);
        make.centerY.equalTo(editIcon);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.left.equalTo(editIcon.mas_right).with.offset(5);
    }];
   
    //分割线
    UIImageView *xian = [[UIImageView alloc]init];
    xian.backgroundColor = DBColor(226, 226, 226);
    [self.view addSubview:xian];
   
    [xian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@1);
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.top.equalTo(editIcon.mas_bottom).with.offset(5);
    }];
   
    //选择标签
    UILabel *tagLabel = [[UILabel alloc]init];
    tagLabel.text = @"选择标签";
    tagLabel.textColor = c3;
    tagLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tagLabel];
   
    [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@20);
        make.width.mas_equalTo(@60);
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.top.equalTo(xian).with.offset(35);
    }];
   
    //跳转标签选择
    UITextField *tagText = [[UITextField alloc]init];
    tagText.placeholder = @"美容颜";
    tagText.borderStyle=UITextBorderStyleRoundedRect;
    tagText.delegate = self;
    [tagText addTarget:self action:@selector(textTag) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:tagText];
   
    [tagText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tagLabel);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.left.equalTo(tagLabel.mas_right).with.offset(5);
    }];
   
    //tagView
    self.tagView = ({
        SKTagView *view = [SKTagView new];
        view.backgroundColor = [UIColor clearColor];
        view.padding    = UIEdgeInsetsMake(0, 0, 0, 0);
        view.insets    = 15;
        view.lineSpace = 10;
        __weak SKTagView *weakView = view;
        view.didClickTagAtIndex = ^(NSUInteger index){
            //Remove tag
            [weakView removeTagAtIndex:index];
        };
        view;
    });
    [self.view addSubview:self.tagView];
    [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.top.equalTo(tagText.mas_bottom).with.offset(10);
    }];
   
    //label标识语
    UILabel *label = [[UILabel alloc]init];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor redColor];
    label.text = @"PS:成员和视频越多得社团越容易被发现!";
    [self.view addSubview:label];
   
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.top.equalTo(self.tagView.mas_bottom).with.offset(20);
    }];
   
    UIButton *commitBtn = [[UIButton alloc]init];
    [commitBtn setCornerRadius:5];
    [commitBtn setBorderWidth:1 color:DBTextThemeColor];
    [commitBtn setTitleColor:DBTextThemeColor forState:UIControlStateNormal];
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [commitBtn setTitle:@"确认发布" forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitButton) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:commitBtn];
   
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@30);
        make.left.equalTo(weakSelf.view).with.offset(10);
        make.right.equalTo(weakSelf.view).with.offset(-10);
        make.top.equalTo(label.mas_bottom).with.offset(50);
    }];
}



文/着小铺(简书作者)
原文链接:http://www.jianshu.com/p/f0b17ecfd04e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

免责声明:文章转载自《iOS开发 Masonry的简单使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇OSG 四元数Quat小程序-广告轮播/控制属性下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Zabbix+Grafana 展示示例1

Zabbix+Grafana 展示示例 Grafana是一个跨平台的开源度量分析和可是化的工具,可以通过该将采集的数据查询然后可视化的展示,并及时通知. 1. Grafana 特性 1. 展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图,折线图,图表等多种展示方式. 2. 数据源: Grap...

C#刷遍Leetcode面试题系列连载(1)

目录 系列教程索引 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + 在线验证后提交 选项2: VS Code本地Debug + 在 LeetCode 插件中验证和提交 安装C#相关插件 配置 .NET...

命令行执行robot framework测试用例

测试套件和测试用例如下图: 1.执行指定目录下的所有suite 执行命令: pybot F:opmsTestCaseTestCase 结果如下:   2.执行指定的suite 执行命令: pybot F:opmsTestCaseTestCase eacher.txt 结果如下:   3.执行suite中的单个要测试用例 语法:pybot --test 用...

ios中的UITextField使用大全

– textRectForBounds:    //重写来重置文字区域 – drawTextInRect:    //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – placeholderRectForBounds:  //重写来重置占位符区域 – drawPlaceholderInRect...

Elasticsearch集群运维

一、索引管理 1、 创建索引 PUT test-2019-03 {          "settings": {                    "index": {                             "number_of_shards": 10,                             "number_of_r...

html5中调用摄像头拍照

方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象。调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像界面指示位置 回调方法: CameraSuccessCallback: 调用摄像头操作成功回调 CameraErrorCallback: 摄像头操作...