服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS自适配利器Masonry使用指南

IOS自适配利器Masonry使用指南

2021-01-05 12:14FlyElephant IOS

如果说自动布局解救了多屏幕适配,那众多三方库的出现就解救了系统自动布局的写法。Masonry就是其中一个。用法上也比较简单灵活,很大程度上替代了传统的NSLayoutConstraint布局方式。下面我们就来具体探讨下吧

关于ios布局自动iphone6之后就是autolayout,autolayout固然非常好用,不过有时候我们需要在页面手动进行页面布局,vfl算是一种选择,而且vfl不复杂,理解起来很容易,实际开发中用的特别熟还好,要是第一次看估计要花点功夫才能搞定。masonry算是vfl的简化版,用的人比较多,之前项目中用过一次,对手动写页面的开发来说算是福利。

基础知识

首先我们看一个常见的问题将一个子view放在的uiviewcontroller的某个位置,通过设置边距来实现,效果如下:

IOS自适配利器Masonry使用指南

如果通过vfl我们代码会是这样的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
uiview *superview                               = self.view;
 
uiview *view1                                   = [[uiview alloc] init];
view1.translatesautoresizingmaskintoconstraints = no;
view1.backgroundcolor                           = [uicolor redcolor];
[superview addsubview:view1];
 
uiedgeinsets padding                            = uiedgeinsetsmake(200, 50, 200, 50);
 
[superview addconstraints:@[
 
                            //约束
                            [nslayoutconstraint constraintwithitem:view1
                                                         attribute:nslayoutattributetop
                                                         relatedby:nslayoutrelationequal
                                                            toitem:superview
                                                         attribute:nslayoutattributetop
                                                        multiplier:1.0
                                                          constant:padding.top],
 
                            [nslayoutconstraint constraintwithitem:view1
                                                         attribute:nslayoutattributeleft
                                                         relatedby:nslayoutrelationequal
                                                            toitem:superview
                                                         attribute:nslayoutattributeleft
                                                        multiplier:1.0
                                                          constant:padding.left],
 
                            [nslayoutconstraint constraintwithitem:view1
                                                         attribute:nslayoutattributebottom
                                                         relatedby:nslayoutrelationequal
                                                            toitem:superview
                                                         attribute:nslayoutattributebottom
                                                        multiplier:1.0
                                                          constant:-padding.bottom],
 
                            [nslayoutconstraint constraintwithitem:view1
                                                         attribute:nslayoutattributeright
                                                         relatedby:nslayoutrelationequal
                                                            toitem:superview
                                                         attribute:nslayoutattributeright
                                                        multiplier:1
                                                          constant:-padding.right],
 
                            ]];

 

只是简单的设置了一个边距,如果视图的关系比较复杂,维护起来会是一个很痛苦的事情,我们看一下masonry是如何实现的,导入masonry.h头文件,约束的代码:

?
1
2
3
4
5
6
7
8
9
uiview  *childview=[uiview new];
[childview setbackgroundcolor:[uicolor redcolor]];
//先将子view加入在父视图中
[self.view addsubview:childview];
__weak typeof(self) weakself = self;
uiedgeinsets padding = uiedgeinsetsmake(200, 50, 200, 50);
[childview mas_makeconstraints:^(masconstraintmaker *make) {
    make.edges.equalto(weakself.view).with.insets(padding);
}];

 

通过mas_makeconstraints设置边距有种鸟枪换炮的感觉,我们即将开启一段新的旅程,可以紧接着看下面比较实用的功能~

实用知识

1.view设置大小

?
1
2
3
4
5
6
7
8
9
10
11
uiview  *childview=[uiview new];
[childview setbackgroundcolor:[uicolor redcolor]];
//先将子view加入在父视图中
[self.view addsubview:childview];
__weak typeof(self) weakself = self;
[childview mas_makeconstraints:^(masconstraintmaker *make) {
    //设置大小
    make.size.mas_equalto(cgsizemake(100, 100));
    //居中
    make.center.equalto(weakself.view);
}];

 

效果如下:

IOS自适配利器Masonry使用指南  

 

这里友情其实一个小内容,目前我们设置约束都是通过mas_makeconstraints用来创建约束,mas_updateconstraints用来更新约束,mas_remakeconstraints重置约束,清除之前的约束,保留最新的约束,如果想深入解释下,可以阅读下面的英文解释~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 *  creates a masconstraintmaker with the callee view.
 *  any constraints defined are added to the view or the appropriate superview once the block has finished executing
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return array of created masconstraints
 */
- (nsarray *)mas_makeconstraints:(void(^)(masconstraintmaker *make))block;
 
/**
 *  creates a masconstraintmaker with the callee view.
 *  any constraints defined are added to the view or the appropriate superview once the block has finished executing.
 *  if an existing constraint exists then it will be updated instead.
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return array of created/updated masconstraints
 */
- (nsarray *)mas_updateconstraints:(void(^)(masconstraintmaker *make))block;
 
/**
 *  creates a masconstraintmaker with the callee view.
 *  any constraints defined are added to the view or the appropriate superview once the block has finished executing.
 *  all constraints previously installed for the view will be removed.
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return array of created/updated masconstraints
 */
- (nsarray *)mas_remakeconstraints:(void(^)(masconstraintmaker *make))block;

2.设置高度,这里设置左右边距,因此不设置宽度,如果想单独设置width可参考高度的设置方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
uiview  *childview=[uiview new];
[childview setbackgroundcolor:[uicolor greencolor]];
//先将子view加入在父视图中
[self.view addsubview:childview];
__weak typeof(self) weakself = self;
[childview mas_makeconstraints:^(masconstraintmaker *make) {
    //距离顶部44
    make.top.equalto(weakself.view.mas_top).with.offset(44);
    //距离左边30
    make.left.equalto(weakself.view.mas_left).with.offset(30);
    //距离右边30,注意是负数
    make.right.equalto(weakself.view.mas_right).with.offset(-30);
    //高度150
    make.height.mas_equalto(@150);
}];

IOS自适配利器Masonry使用指南

 

3.子视图之间的位置设置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
uiview  *childview=[uiview new];
[childview setbackgroundcolor:[uicolor greencolor]];
//先将子view加入在父视图中
[self.view addsubview:childview];
__weak typeof(self) weakself = self;
[childview mas_makeconstraints:^(masconstraintmaker *make) {
    //距离顶部44
    make.top.equalto(weakself.view.mas_top).with.offset(44);
    //距离左边30
    make.left.equalto(weakself.view.mas_left).with.offset(30);
    //距离右边30,注意是负数
    make.right.equalto(weakself.view.mas_right).with.offset(-30);
    //高度150
    make.height.mas_equalto(@150);
}];
//地址:http://www.cnblogs.com/xiaofeixiang/
uiview *nextview=[uiview new];
[nextview setbackgroundcolor:[uicolor redcolor]];
[self.view addsubview:nextview];
[nextview mas_makeconstraints:^(masconstraintmaker *make) {
    make.top.equalto(childview.mas_bottom).with.offset(30);
    make.right.equalto(childview.mas_right).with.offset(-30);
    make.width.mas_equalto(@100);
    make.height.mas_equalto(@100);
}];

IOS自适配利器Masonry使用指南

4.链式写法,算是一个便利的写法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    uiview  *childview=[uiview new];
    [childview setbackgroundcolor:[uicolor greencolor]];
    //先将子view加入在父视图中
    [self.view addsubview:childview];
    __weak typeof(self) weakself = self;
    [childview mas_makeconstraints:^(masconstraintmaker *make) {
        make.top.and.left.mas_equalto(weakself.view).with.offset(100);
        make.bottom.and.right.mas_equalto(weakself.view).with.offset(-100);
        //第二种写法更简单,相对于就是父视图
//        make.top.and.left.mas_equalto(100);
//        make.bottom.and.right.mas_equalto(-100);
    }];
     
    uilabel *label=[uilabel new];
    [label settext:@"博客园-flyelephant"];
    [label settextcolor:[uicolor redcolor]];
    [label settextalignment:nstextalignmentcenter];
    [self.view addsubview:label];
    [label mas_makeconstraints:^(masconstraintmaker *make) {
        make.left.mas_equalto(weakself.view).with.offset(10);
        make.height.mas_equalto(20);
        make.right.mas_equalto(weakself.view).with.offset(-10);
        make.bottom.mas_equalto(weakself.view).with.offset(-50);
    }];

IOS自适配利器Masonry使用指南

 网上关于masonry的教程很多,给的例子的也很多,这几种情况基本上满足了开发中的需求,不会有太多的出入,算是一个简易版的教程,masonry的中属性和ios中的属性是有对应的关系,不过因为很简单,基本上没怎么看,下图是一个对照关系:

IOS自适配利器Masonry使用指南

总结:

  1. 可以给控件添加left/right/top/bottom/size/height/width/insert约束;
  2. 库提供了三个方法,mas_makeconstraints添加约束,mas_updateconstraints修改约束,mas_remakeconstraints清除以前约束并添加新约束;
  3. 可以通过view.mas_bottom获得view的某个约束;
  4. 在约束的block中,使用make来给当前控件添加约束。

延伸 · 阅读

精彩推荐
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04