Autolayout基础

作者: 习惯有你syh | 来源:发表于2017-11-24 16:29 被阅读28次

1、什么是Autolayout?

答: Autolayout是一种“自动布局”技术,专门用来布局UI界面的。Autolayout自iOS6开始引入,由于Xcode4的不给力,当时并没有得到很大推广。自iOS7(Xcode5)开始,Autolayout的开发效率得到很大的提升。苹果官方也推荐开发者尽量使Autolayout来布局UI界面,它能很轻松地解决屏幕适配的问题。

上边这段是在别处复制的,没有上边这段总觉着少点什么......各位看官勿怪。

2、Autolayout的两个核心概念(约束、参照物)。

  • 约束:对View的大小和位置进行约束;

  • 参照物:以某个View作为参照物对目标View添加约束。

Autolayout就是利用约束和参照物来控制视图的大小和位置,系统会在运行时根据你设置的约束进行计算得到具体的Frame再绘制屏幕。
一般4个约束就能确定控件的具体位置和大小,不能多也不能少(能根据控件内容计算高度的控件除外。比如,UILabel、UITextView、UIImage)。

3、Autolayout用到的核心类(NSLayoutConstraint)

这个类点开它的头文件,内容很简单,这里把经常用到的属性、方法、枚举拿出来说一说。

(1)、类方法,添加一个约束
/**
    给视图添加一个约束;
    @param   view1   目标View;
    @param   attr1   目标View约束的属性,是一个枚举(NSLayoutAttribute),这个枚举点进去一看知;
    @param   relation    与目标View约束值得关系(>、=、<),也是一个枚举(NSLayoutRelation);
    @param   view2   参照物View;
    @param   attr2     参照物View约束的属性;
    @param   multiplier  间距的倍数;
    @param   c 间距的具体数值;
    return   一个约束(NSLayoutConstraint对象)。
*/
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
(2)、priority属性(优先级)

@property UILayoutPriority priority;

这个属性用来修饰约束的优先级,它的取值范围为0.0 ~ 1000.0,优先级越高,该约束优先生效。一个约束的默认优先级为最高,也就是1000.0。系统给我们提供了四个优先级:

UILayoutPriorityRequired   -->   值为1000,最高优先级,约束默认优先级;

UILayoutPriorityDefaultHigh  -->  值为750,高优先级;

UILayoutPriorityDefaultLow   -->   值为250,低优先级;

UILayoutPriorityFittingSizeLevel   -->  值为50,极低优先级,但不是最低,优先级最低值为0.0。

这四个等级完全可以满足日常开发中大部分的需求。当然你也可以在0.0 ~ 1000.0随意设置它的值,但不能超过这个范围。

如果超出这个范围,编译是可以通过的,但是运行时会crash。

4、上代码

(1)、首先我们要实现以下的效果,红色View距离父View左边和上边20,自身宽和高为50。


redView.png
//添加一个距离父View左边距离为20的约束,参照物是父View
NSLayoutConstraint *redViewLeftC = [NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeLeftMargin) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeLeftMargin multiplier:1.0f constant:20.0];
//有参照物,所以这个约束要添加到父View上
[self.view addConstraint:redViewLeftC];
    
//添加一个距离父View上边距离为20的约束,参照物是父View
NSLayoutConstraint *redViewTopC = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1.0f constant:20.0];
//有参照物,所以这个约束要添加到父View上
[self.view addConstraint:redViewTopC];
    
//添加一个宽度为50的约束,参照物是自己
NSLayoutConstraint *redViewWidthC = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
//参照物是自己,添加到目标View上
[redView addConstraint:redViewWidthC];
    
//添加一个高度为50的约束,参照物是自己
NSLayoutConstraint *redHeightC = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
//参照物是自己,添加到目标View上
[redView addConstraint:redHeightC];
(2)、实现以下效果,添加一个蓝色View,左边距离红色View20,上边与红色View水平对其,宽高与红色View相同。 redViewAndBlueView.png
    //创建蓝色View
    UIView *blueView = [[UIView alloc]init];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    self.blueView = blueView;

    //添加一个距离红色View右边距离为20的约束,参照物是redView
    NSLayoutConstraint *blueViewLeftC = [NSLayoutConstraint constraintWithItem:blueView attribute:(NSLayoutAttributeLeftMargin) relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeRightMargin multiplier:1.0f constant:20.0];
    //参照物是redView,添加到目标父View上
    [self.view addConstraint:blueViewLeftC];
    
    //添加一个与红色View上边水平对齐的约束,参照物是redView
    NSLayoutConstraint *blueViewTopC = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeTopMargin multiplier:1.0f constant:0.0];
    //参照物是redView,添加到目标父View上
    [self.view addConstraint:blueViewTopC];
    
    //添加一个宽度为50的约束,参照物是自己
    NSLayoutConstraint *blueViewWithC = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
    //参照物是自己,添加到目标View上
    [blueView addConstraint:blueViewWithC];
    
    //添加一个宽度为50的约束,参照物是自己
    NSLayoutConstraint *blueViewHeightC = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
    //参照物是自己,添加到目标View上
    [blueView addConstraint:blueViewHeightC];

(3)、接下来利用优先级 UILayoutPriority 实现一个简单的动画效果,点击View


11月-24-2017 16-12-44.gif
//创建黄色View
    UIView *yellowView = [[UIView alloc]init];
    yellowView.backgroundColor = [UIColor yellowColor];
    yellowView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:yellowView];
    
    //添加一个距离蓝色View右边距离为20的约束,参照物是blueView
    NSLayoutConstraint *yellowVLeftBlueV = [NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRightMargin multiplier:1.0f constant:20.0];
    //参照物是blueView,添加到目标父View上
    [self.view addConstraint:yellowVLeftBlueV];
    
    //添加一个与蓝色View上边水平对齐的约束,参照物是blueView
    NSLayoutConstraint *yellowViewTopC = [NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeTopMargin multiplier:1.0f constant:0.0];
    //参照物是blueView,添加到目标父View上
    [self.view addConstraint:yellowViewTopC];
    
    //添加一个宽度为50的约束,参照物是自己
    NSLayoutConstraint *yellowViewWidthC = [NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
    //参照物是自己,添加到目标View上
    [yellowView addConstraint:yellowViewWidthC];
    
    //添加一个高度为50的约束,参照物是自己
    NSLayoutConstraint *yellowViewHeightC = [NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0f constant:50.0];
    //参照物是自己,添加到目标View上
    [yellowView addConstraint:yellowViewHeightC];
    
    //添加一个距离红色View右边20的约束,参照物是redView
    NSLayoutConstraint *yellowVLeftRedVC = [NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeRightMargin multiplier:1.0f constant:20.0];
    //将他的优先级的调整到UILayoutPriorityDefaultHigh
    yellowVLeftRedVC.priority = UILayoutPriorityDefaultHigh;
    //参照物是redView,添加到目标父View上
    [self.view addConstraint:yellowVLeftRedVC];

以上内容是Autolayout最基础的知识,下一期说一说Masonry的使用。

相关文章

  • 9.6AutoLayout约束基础

    AutoLayout约束基础 AutoLayout约束基础1.png AutoLayout约束基础2.png Au...

  • Autolayout基础

    1、什么是Autolayout? 答: Autolayout是一种“自动布局”技术,专门用来布局UI界面的。Aut...

  • Autolayout 基础

    https://www.zybuluo.com/MicroCai/note/73867

  • 手把手教你AutoLayout适配复杂界面《一》

    基础篇,大神请绕道!!!AutoLayout是什么?使用一句Apple的官方定义的话 AutoLayout是一种基...

  • 基础 (十二) : Autolayout

    适配 什么是适配? 适应、兼容各种不同的情况 移动开发中,适配的常见种类 系统适配 针对不同版本的操作系统进行适配...

  • 【iOS】AutoLayout基础篇

    AutoLayout是iOS6开始引入的一种布局技术,专门用来做UI界面布局的。用来解决Frame布局在多屏幕尺寸...

  • UI基础8 Autolayout

    使用代码实现Autolayout的方法1 创建约束 添加约束 注意一定要在拥有父控件之后再添加约束关闭Autore...

  • UI基础—实现AutoLayout

    使用代码实现Autolayout的方法1 创建约束 添加约束 注意一定要在拥有父控件之后再添加约束关闭Autore...

  • Masonry初探

    Masonry整理 Masonry是以AutoLayout为基础的轻量级布局框架更加简化了整个约束系统 Mason...

  • 9.4 AutoLayout使用

    AutoLayout使用 AutoLayout使用.png

网友评论

    本文标题:Autolayout基础

    本文链接:https://www.haomeiwen.com/subject/anxfbxtx.html