AutoLayout概念是苹果自iOS6开始引入的概念。
目前为止,实现自动布局技术选型方面也可以使用xib和storyboard。在开发过程中通常登录、注册等变动可能性较小的视图,我会采用xib开发,其他页面通常会采用Masonry布局。xib和手码各有优势,视情况而定。
 
关于NSLayoutAttributeLeading和NSLayoutAttributeTrailing,前边和后边并不是总是为左边和右边的,有些国家的前边是右边后边是左边所以这样设定是为了国际化考虑。还有视图基准线NSLayoutAttributeBaseline通常是指视图的底部放文字的地方。
 
使用NSLayoutConstraint之前,我们需要确定一点:为了防止constraint和view本身的autoresizing属性冲突,我们需要设置view的属性:

view.translatesAutoresizingMaskIntoConstraints = NO;

一、UIKit框架提供的自动布局的方法一,详细请看参数介绍:

/**
设置约束 @param view1 指定需要添加约束的视图一
@param attr1 指定视图一需要约束的属性
@param relation 指定视图一和视图二添加约束的关系
@param view2 指定视图一依赖关系的视图二;可为nil
@param attr2 指定视图一所依赖的视图二的属性,若view2=nil,该属性设置 NSLayoutAttributeNotAnAttribute
@param multiplier 系数 情况一和二为亲测
情况一:设置A视图的高度 = A视图高度 * multiplier + constant;此时才会起作用;
情况二:设置A视图和其他视图的关系或 toItem=nil,multiplier设置不等于0即可,若等于0会crash;
@param c 常量
@return 返回生成的约束对象
*/
+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

以上函数设置的约束等价于   view1.att1 =(以等号举栗子,relation可> < ≥≤)view2.attr2 * multiplier + c;

简单小需求:随意在控制器中添加一个红色view:

// 设置 redView 的宽 = 100.f * 1.f
NSLayoutConstraint *redViewWidthConstraint =
[NSLayoutConstraint constraintWithItem:self.redView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:.f
constant:.f];
// 设置 redView 的高 = 100.f * 1.f
NSLayoutConstraint *redViewHeightConstraint =
[NSLayoutConstraint constraintWithItem:self.redView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:.f
constant:.f];
// 设置 redView 的前面(左边) = self.view.leading + 20.f
NSLayoutConstraint *redViewleadingConstraint =
[NSLayoutConstraint constraintWithItem:self.redView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:.f
constant:.f]; // 设置 redView 的顶部
NSLayoutConstraint *redViewTopConstraint =
[NSLayoutConstraint constraintWithItem:self.redView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:.f
constant:.f];
[self.view addConstraint:redViewWidthConstraint];
[self.view addConstraint:redViewHeightConstraint];
[self.view addConstraint:redViewleadingConstraint];
[self.view addConstraint:redViewTopConstraint];

小注:
1、关于参数  multiplier 
✅举个栗子												

自动布局之-NSLayoutConstraint的更多相关文章

  1. IOS页面自动布局 之 NSLayoutConstraint基础篇

    使用AutoLayout之前需要知道以下两点: 1.必须设置 translatesAutoresizingMaskIntoConstraints为NO. 2.如果是viewControl则AutoLa ...

  2. NSLayoutConstraint 使用详解 VFL使用介绍

    注意 使用前必须先取消所有的你想设置View 的 Autoresizing 属性 因为 Autoresizing  Layout不能共存  系统默认是 Autoresizing for v in su ...

  3. iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant

    /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * mul ...

  4. iOS原生自动布局NSLayoutConstraint

    AutoLayout概念是苹果自iOS6开始引入的概念. 目前为止,实现自动布局技术选型方面也可以使用xib和storyboard.在开发过程中通常登录.注册等变动可能性较小的视图,我会采用xib开发 ...

  5. iOS开发NSLayoutConstraint代码自动布局

    1.NSLayoutConstraint简介 适配界面大多用Masonry工具,也是基于NSLayoutConstraint写的!通过使用两个类方法实现自动布局: + (NSArray<__ki ...

  6. 使用autolayout的NSLayoutConstraint类中的constraintWithItem 、constraintsWithVisualFormat这两个类方法来创建视图并可以实现自动布局

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  7. Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇

    要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处 /* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的 控件约束那么有父控件添加 *创建约束 NSLayo ...

  8. AutoLayout自动布局,NSLayoutConstraint 视图约束使用

    一.方法 NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:<#(id)#> attribut ...

  9. (翻译)开始iOS 7中自动布局教程(二)

    这篇教程的前半部分被翻译出来很久了,我也是通过这个教程学会的IOS自动布局.但是后半部分(即本篇)一直未有翻译,正好最近跳坑翻译,就寻来这篇教程,进行翻译.前半部分已经转载至本博客,后半部分即本篇.学 ...

随机推荐

  1. JavaScript和jquery中的宽度

    Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...

  2. Python之PIP安装

    Python有两个著名的包管理工具easy_install.py和pip.Python2.7的安装包中自带了easy_install.py,而pip需要手动安装.而在Python3.5之后都是默认安装 ...

  3. Apache服务器配置虚拟域名

    我在别处发的帖子 http://www.52pojie.cn/thread-599829-1-1.html

  4. mycat学习日记:关于联表查询

    https://www.cnblogs.com/toulon/p/4832895.html 在使用数据库中间件之前,我就想到分库分表的操作对于联表操作可能会显得非常复杂.因为如果数据是分片存储的,如果 ...

  5. Python lambda(匿名函数)介绍【转】

    引用: http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...

  6. js事件循环(event loop)

    我们都知道,js是单线程的,虽然现在有 worker 的存在,但是也只是可以进行运算,并不能操作 dom: js最一开始执行的线程,是主线程,然后主线程执行完毕后,是微队列 microtask 的循环 ...

  7. CXF 发布rest服务

    1.1      什么是rest服务 REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位. Rest要求对资源定位更加准 ...

  8. HDU 5505——GT and numbers——————【素数】

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. nginx fpm生产环境的权限设置

    http://www.2cto.com/Article/201307/231770.html

  10. Ubuntu下eclipse无法识别手机驱动(以小米2S为例子)

    google官方开发向导里对Android手机已经设置了允许安装非market程序,并且处于usb调试模式,但是仍然在usb连接电脑后无法被识别的问题作了解释. 在Ubuntu Linux环境下需要添 ...