Autolayout详解


AutoLayout相关概念

  • 参照:参照对象;
  • 约束:限制的一些条件;

使用代码创建约束时的注意点:

  • 必须关闭Autoresizing,因为它们两者会互相冲突:
 blueView.translatesAutoresizingMaskIntoConstraints = NO;

通过Storyboard、xib来进行自动布局

  • 设置AutoLayout的相关界面:


    界面1

    界面2

    界面3

    界面3

警告与错误:

  • 警告:

控件的frame不匹配所添加的约束, 比如:约束控件的宽度为100, 而控件现在的宽度是110;

  • 错误:

1、缺乏必要的约束, 比如:只约束了宽度和高度, 没有约束具体的位置
2、两个约束冲突, 比如:1个约束控件的宽度为100, 1个约束控件的宽 度为110

  • 在Xcode右边可以看到一个控件相关的约束:


    约束界面
    • 双击或作点击Edit可以修改约束相关的参数,也可以用一条核心公式来表达Autolayout:

obj1.property1 =(obj2.property2 * multiplier)+ constant value
解释:obj1的property1属性等于obj2的property2属性乘以multiplier(系数)再加constant(常量);

核心公式
  • 注意:priority表示优先级,取值区间为[0,1000],值越大,表示优先级越高,会优先执行

添加约束的规则

  • 1.在创建约束之后,需要将其添加到作用的view上
    在添加时要注意目标view需要遵循以下规则:
    对于两个同层级view之间的约束关系,添加到它们的父view上,图示:


    规则1
  • 2.对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上,图示:


    规则2
  • 3.对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上,图示


    规则3

使用Autolayout实现UILabel内容包裹的步骤:

  • 1、设置UILabel的上,左,右的约束
  • 2、设置显示行数为0(目的为了能让字体自动换行)


    默认效果

    内容自动包裹

使用Autolayout来实现动画功能:

  • 在执行动画时记得调用一下方法:
//在修改了约束之后,只要执行下面代码,就能做动画效果
[UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];

用第三方框架实现Autolayout功能

  • 由于苹果官方通过代码实现Autolayout是很麻烦的,往往一条约束就需要显示三行代码,例如:
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
//关闭Autoresizing
blueView.translatesAutoresizingMaskIntoConstraints = NO;
//创建左边约束
NSLayoutConstraint *leftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftLc];
    
//创建右边约束
NSLayoutConstraint *rightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [self.view addConstraint:rightLc];
    
//创建底部约束
NSLayoutConstraint *bottomLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
    [self.view addConstraint:bottomLc];
    
//创建高度约束
NSLayoutConstraint *heightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
    [blueView addConstraint: heightLc];

效果图:

效果图
  • VFL语言

  • 苹果推出的相对简单的,用来进行自动布局的抽象语言,但相比代码来说,还有局限性,这里不做介绍

  • Masonry框架(主流)

    • 目前最流行的Autolayout第三方框架
    • 用优雅的代码方式编写Autolayout
    • 省去了苹果官方恶心的Autolayout代码
    • 大大提高了开发效率
  • 实例1:

//创建蓝色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];

//创建红色控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

[blueView makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(redView.width).offset(0);
    make.height.equalTo(redView.height).offset(0);
    make.height.equalTo(100);
    make.left.equalTo(blueView.superview.left).offset(20);
    make.bottom.equalTo(blueView.superview.bottom).offset(-20);
    make.right.equalTo(redView.left).offset(-20);
}];

[redView makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(redView.superview.bottom).offset(-20);
    make.right.equalTo(redView.superview.right).offset(-20);
}];
  • 实例1的效果:


  • 实例2:

UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
UIView *blueView1 = [[UIView alloc] init];
blueView1.backgroundColor = [UIColor lightGrayColor];
[blueView addSubview:blueView1];
UIView *blueView2 = [[UIView alloc] init];
blueView2.backgroundColor = [UIColor blackColor];
[blueView addSubview:blueView2];


UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *redView1 = [[UIView alloc] init];
redView1.backgroundColor = [UIColor lightGrayColor];
[redView addSubview:redView1];
UIView *redView2 = [[UIView alloc] init];
redView2.backgroundColor = [UIColor blackColor];
[redView addSubview:redView2];

UIView *greenView = [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
UIView *greenView1 = [[UIView alloc] init];
greenView1.backgroundColor = [UIColor lightGrayColor];
[greenView addSubview:greenView1];
UIView *greenView2 = [[UIView alloc] init];
greenView2.backgroundColor = [UIColor blackColor];
[greenView addSubview:greenView2];

UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangeView];
UIView *orangeView1 = [[UIView alloc] init];
orangeView1.backgroundColor = [UIColor lightGrayColor];
[orangeView addSubview:orangeView1];
UIView *orangeView2 = [[UIView alloc] init];
orangeView2.backgroundColor = [UIColor blackColor];
[orangeView addSubview:orangeView2];


[blueView makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(redView.width).offset(0);
    make.height.equalTo(redView.height).offset(0);
    make.width.equalTo(greenView.width).offset(0);
    make.height.equalTo(greenView.height).offset(0);
    make.width.equalTo(orangeView.width).offset(0);
    make.height.equalTo(orangeView.height).offset(0);
    make.top.left.equalTo(blueView.superview).offset(0);
    make.bottom.equalTo(orangeView.top).offset(0);
    make.right.equalTo(redView.left).offset(0);
}];

[redView makeConstraints:^(MASConstraintMaker *make) {
    make.top.right.equalTo(redView.superview).offset(0);
    make.bottom.equalTo(greenView.top).offset(0);
}];

[greenView makeConstraints:^(MASConstraintMaker *make) {
    make.right.bottom.equalTo(greenView.superview).offset(0);
    make.left.equalTo(orangeView.right).offset(0);
}];

[orangeView makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.equalTo(orangeView.superview).offset(0);
}];

[blueView1 makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(blueView1.superview.center).offset(0);
    make.width.height.equalTo(100);
}];

[redView1 makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(redView1.superview.center).offset(0);
    make.width.height.equalTo(100);
}];

[greenView1 makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(greenView1.superview.center).offset(0);
    make.width.height.equalTo(100);
}];

[orangeView1 makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(orangeView1.superview.center).offset(0);
    make.width.height.equalTo(100);
}];

[blueView2 makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.right.equalTo(blueView2.superview).offset(0);
    make.height.equalTo(30);
}];

[redView2 makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.right.equalTo(redView2.superview).offset(0);
    make.height.equalTo(30);
}];

[greenView2 makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.right.equalTo(greenView2.superview).offset(0);
    make.height.equalTo(30);
}];

[orangeView2 makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.right.equalTo(orangeView2.superview).offset(0);
    make.height.equalTo(30);
}];
  • 实例2的效果:


  • 实例3:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test2:1];
}

- (void)test1
{
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    UIView *orangeView = [[UIView alloc] init];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    
    UIView *greenView = [[UIView alloc] init];
    greenView.backgroundColor = [UIColor greenColor];
    [orangeView addSubview:greenView];
    
    
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blueView.superview.left).with.offset(20);
        make.right.equalTo(blueView.superview.right).with.offset(-20);
        make.top.equalTo(blueView.superview.top).with.offset(20);
        make.height.equalTo(100);
    }];
    
    [redView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(blueView.right).offset(0);
        make.top.equalTo(blueView.bottom).offset(20);
        make.height.equalTo(blueView.height);
        make.width.equalTo(blueView.width).multipliedBy(0.5);
    }];
    
    [orangeView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blueView.left).offset(0);
        make.right.equalTo(redView.left).offset(-20);
        make.top.equalTo(redView.top);
        make.height.equalTo(redView.height).multipliedBy(2).offset(0);
    }];
    
    [greenView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(greenView.superview.centerX).offset(0);
        make.centerY.equalTo(greenView.superview.centerY).offset(0);
        make.width.equalTo(greenView.superview.width).multipliedBy(0.5);
        make.height.equalTo(greenView.superview.height).multipliedBy(0.5);
    }];
}
/**
 *  产生0~10个随机色的View,它们的间距为20
 *
 *  @param num View的个数
 */
- (void)test2:(int)num
{
//    int count = num;
    int margin = arc4random_uniform(20);
    
    for (int i = 0; i<num; i++) {
        UIView *greenView = [[UIView alloc] init];
        greenView.backgroundColor = [UIColor randomColor];
        [self.view addSubview:greenView];
        
        [greenView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(greenView.superview.left).offset(i *margin);
            make.right.equalTo(greenView.superview.right).offset(-i *margin);
            make.top.equalTo(greenView.superview.top).offset(i *margin);
            make.bottom.equalTo(greenView.superview.bottom).offset(-i *margin);
        }];
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self test2:arc4random_uniform(12)];
}
  • 实例3的test1方法实现效果:


    test1
  • 实例3的test2方法实现效果:


    test2

Autoresizing

- UIViewAutoresizingFlexible`Left`Margin   = 1 << 0,
    - 距离父控件`左边`的间距是伸缩的
- UIViewAutoresizingFlexible`Right`Margin  = 1 << 2,
    - 距离父控件`右边`的间距是伸缩的
- UIViewAutoresizingFlexible`Top`Margin    = 1 << 3,
    - 距离父控件`上边`的间距是伸缩的
- UIViewAutoresizingFlexible`Bottom`Margin = 1 << 5
    - 距离父控件`下边`的间距是伸缩的
- UIViewAutoresizingFlexible`Width`        = 1 << 1,
    - `宽度`跟随父控件`宽度`进行伸缩
- UIViewAutoresizingFlexible`Height`       = 1 << 4,
    - `高度`跟随父控件`高度`进行伸缩
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

  • 教程开始前的废话连篇 在iphone5出现之后,也就是ios6的时候。约束(autolayout)出现了,其实最开...
    Lecturer阅读 28,629评论 7 58
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,612评论 4 59
  • 一、适配基础准则: 1、在一些比较简单、固定的界面。比如登录、注册或者其他只是进行内容展示的界面使用XIB、Sto...
    iOS白水阅读 592评论 0 0
  • 前言 iPhone自诞生以来,随着其屏幕尺寸不断的多样化,屏幕适配的技术一直在发展更新。目前,iOS系统版本已经更...
    VV木公子阅读 15,233评论 24 170
  • 为了弄明白日更到底有没有用,我坚持日更了21天,今天是最后一天,写下自己的思考。 关于日更简书上有两种观点:一种认...
    黑色火阅读 377评论 1 13