iOS项目开发中常用到的自动布局技术----- Masonry

来源:互联网 发布:关于网络的名言 编辑:程序博客网 时间:2024/06/04 00:33

前言

 1 MagicNumber -> autoresizingMask -> autolayout 

以上是纯手写代码所经历的关于页面布局的三个时期

  • 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了

  • 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变

  • 在iphone5-iphone5s时代 window的size变了(320,568) 这时autoresizingMask派上了用场(为啥这时候不用Autolayout? 因为还要支持ios5呗) 简单的适配一下即可

  • 在iphone6+时代 window的width也发生了变化(相对5和5s的屏幕比例没有变化) 终于是时候抛弃autoresizingMask改用autolayout了(不用支持ios5了 相对于屏幕适配的多样性来说autoresizingMask也已经过时了)

那如何快速的上手autolayout呢? 说实话 当年ios6推出的同时新增了autolayout的特性 我看了一下官方文档和demo 就立马抛弃到一边了 因为实在过于的繁琐和啰嗦(有过经验的朋友肯定有同感)

直到iphone6发布之后 我知道使用autolayout势在必行了 这时想起了以前在浏览Github看到过的一个第三方库Masonry 在花了几个小时的研究使用后 我就将autolayout掌握了(重点是我并没有学习任何的官方文档或者其他的关于autolayout的知识) 这就是我为什么要写下这篇文章来推荐它的原因。

介绍

Masonry 源码

Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持iOS 和 Max OS X

我们先来看一段官方的sample code来认识一下Masonry

 1 [view1 mas_makeConstraints:^(MASConstraintMaker *make) { 2 make.edges.equalTo(superview).with.insets(padding); 3 }]; 

看到block里面的那句话: make edges equalTo superview with insets
通过链式的自然语言 就把view1给autolayout好了 是不是简单易懂? 

使用

看一下Masonry支持哪一些属性

复制代码
 1 @property (nonatomic, strong, readonly) MASConstraint *left; 2 @property (nonatomic, strong, readonly) MASConstraint *top; 3 @property (nonatomic, strong, readonly) MASConstraint *right; 4 @property (nonatomic, strong, readonly) MASConstraint *bottom; 5 @property (nonatomic, strong, readonly) MASConstraint *leading; 6 @property (nonatomic, strong, readonly) MASConstraint *trailing; 7 @property (nonatomic, strong, readonly) MASConstraint *width; 8 @property (nonatomic, strong, readonly) MASConstraint *height; 9 @property (nonatomic, strong, readonly) MASConstraint *centerX;10 @property (nonatomic, strong, readonly) MASConstraint *centerY;11 @property (nonatomic, strong, readonly) MASConstraint *baseline;
复制代码

 

这些属性与NSLayoutAttrubute的对照表如下

 

其中leading与left trailing与right 在正常情况下是等价的 但是当一些布局是从右至左时(比如阿拉伯文?没有类似的经验) 则会对调 换句话说就是基本可以不理不用 用left和right就好了

在ios8发布后 又新增了一堆奇奇怪怪的属性(有兴趣的朋友可以去瞅瞅) Masonry暂时还不支持(不过你要支持ios6,ios7 就没必要去管那么多了)

在讲实例之前 先介绍一个MACRO

 1 #define WS(weakSelf) __weak __typeof(&*self)weakSelf = self; 

快速的定义一个weakSelf 当然是用于block里面啦 下面进入正题(为了方便 我们测试的superView都是一个size为(300,300)的UIView)

下面 通过一些简单的实例来简单介绍如何轻松愉快的使用Masonry:

那么在使用之前我们需要导入Masonry框架,这里我使用的是cocopoads来给项目加入Masonry,对于不会使用cocoapods的请点击此链接来配置 点此跳转配置

1.  显示三个view

//

//  ViewController.m

//  iOS_自动布局之masonry

//

//  Created by 高宇 on 16/7/5.

//  Copyright © 2016 高宇. All rights reserved.

//


#import "ViewController.h"

#import


@interface ViewController ()


@end


@implementation ViewController



- (UIView *)view1

{

    if (_view1 == nil) {

        _view1 = [[UIView allocinit];

        _view1.backgroundColor = [UIColor purpleColor];

    }

    return _view1;

}


- (UIView *)view2

{

    if (_view2 == nil) {

        _view2 = [[UIView allocinit];

        _view2.backgroundColor = [UIColor yellowColor];

    }

    return _view2;

}


- (UIView *)view3

{

    if (_view3 == nil) {

        _view3 = [[UIView allocinit];

        _view3.backgroundColor = [UIColor redColor];

    }

    return _view3;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self.view addSubview:self.view1];

    [self.view addSubview:self.view2];

    [self.view addSubview:self.view3];

    [self setFrame];

}


- (void)setFrame

{

   

    

    UIView *superView = self.view1.superview;

    

    [self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(superView).with.offset(10);

        make.left.equalTo(superView).with.offset(10);

        make.width.equalTo(self.view2);

        make.height.equalTo(self.view3);


    }];

    

    [self.view2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.view1);

        make.left.equalTo(self.view1.mas_right).with.offset(10);

        make.right.equalTo(superView).with.offset(-10);

        make.bottom.equalTo(self.view1.mas_bottom);

        make.width.equalTo(self.view1);

    }];

    

    [self.view3 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.view1.mas_bottom).with.offset(10);

        make.left.equalTo(superView).with.offset(10);

        make.right.equalTo(superView).with.offset(-10);

        make.bottom.equalTo(superView).with.offset(-10);

        make.height.equalTo(self.view1);

    }];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

代码就这些,其实没有什么难的,运行结果如图:


怎么样,不仅仅是对竖屏进行适配,还对手机横屏进行了适配,是不是很简单!

2. [中级] 在UIScrollView顺序排列一些view并自动计算contentSize

不多加述说了,直接上代码给你们看看:

//

//  ViewController.m

//  iOS_自动布局之masonry的中级使用

//

//  Created by 高宇 on 16/7/5.

//  Copyright © 2016 高宇. All rights reserved.

//


#import "ViewController.h"

#import


@interface ViewController ()


@end


@implementation ViewController


- (UIScrollView *) scrollView

{

    if (_scrollView == nil) {

        _scrollView = [UIScrollView new];

        _scrollView.delegate = self;

    }

    return _scrollView;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view addSubview:self.scrollView];

    [self setFrame];

}


- (void)setFrame

{

    UIView *superView = self.scrollView.superview;

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(superView).width.insets(UIEdgeInsetsMake(10101010));

    }];

    

    

    UIView *Container = [UIView new];

//    container.backgroundColor = [UIColor redColor];

    [self.scrollView addSubview:container];

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.scrollView);

        make.width.equalTo(self.scrollView);

    }];

    

    int count = 10;

    UIView *lastView = nil;

    for ( int i = 1 ; i <= count ; ++i )

    {

        UIView *subv = [UIView new];

        [container addSubview:subv];

        subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )

                                          saturation:( arc4random() % 128 / 256.0 ) + 0.5

                                          brightness:( arc4random() % 128 / 256.0 ) + 0.5

                                               alpha:1];

        [subv mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.and.right.equalTo(container);

            make.height.mas_equalTo(@(20*i));

            if ( lastView )

            {

                make.top.mas_equalTo(lastView.mas_bottom);

            }

            else{

                make.top.mas_equalTo(container.mas_top);

            }}];

        lastView = subv;

    }

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(lastView.mas_bottom);

    }];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

代码是不是很少,运行结果如图所示:

scrollView是可以滑动的额,是不是很简单呀,祝大家在iOS这条路上越走越好,谢啦!
1 0
原创粉丝点击