一.方法

 NSLayoutConstraint *constraint =  [NSLayoutConstraint constraintWithItem:<#(id)#> attribute:<#(NSLayoutAttribute)#> relatedBy:<#(NSLayoutRelation)#> toItem:<#(id)#> attribute:<#(NSLayoutAttribute)#> multiplier:<#(CGFloat)#> constant:<#(CGFloat)#>]
Item:要约束的控件
attribute:要约束的控件约束的类型(做怎样的约束)
relatedBy:与参照控件之间的关系
toItem:参照的控件
attribute:参照的控件约束的类型(做怎样的约束)
multiplier:约束的控件和参照的控件关系倍数
constant:常量
最后两个参数计算关系:
view1.property1 =(view2.property2 * multiplier)+ constant 

二.Autolayout的常见警告和错误

1.警告
控件的frame不匹配所添加的约束, 比如:约束控件的宽度为150, 而控件现在的宽度是160
 
2.错误
缺乏必要的约束, 比如:只约束了宽度和高度, 没有约束具体的位置
两个约束冲突,比如:1个约束控件的宽度为150, 1个约束控件的宽度为160

三.例子

1.代码实现:

ViewController.m

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad];
//创建添加blueview
UIView *blueView=[[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;//禁止Autoresizing
[self.view addSubview:blueView]; //创建添加redView
UIView *redView=[[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView]; // 1.0父View左边 约束 blueView左边 30
NSLayoutConstraint *blueLeftCon=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30.0];
[self.view addConstraint:blueLeftCon]; // 1.1父View顶部 约束 blueView顶部 30
NSLayoutConstraint *blueTopCon=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:30.0 ];
[self.view addConstraint:blueTopCon]; // 1.2父View右边 约束 blueView右边 30
NSLayoutConstraint *blueRightCon= [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:- ];
[self.view addConstraint:blueRightCon]; // 1.3 blueView 相当于 高度设置成50
NSLayoutConstraint *blueHeightCon=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute: multiplier: constant:];
[blueView addConstraint:blueHeightCon]; //2.0 redView 顶部 和 blueView 底部 间距20
NSLayoutConstraint *redTopCon=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:];
[self.view addConstraint:redTopCon]; //2.1 redView 左边 和 blueView 水平中心线对齐
NSLayoutConstraint *redLeftCon=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:];
[self.view addConstraint:redLeftCon]; //2.2 redView 右边 参照 blueView 右边对齐
NSLayoutConstraint *redRightCon = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:];
[self.view addConstraint:redRightCon]; //2.3 redView 高度 参照 blueView 高度相等
NSLayoutConstraint *redHeightCon=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
[self.view addConstraint:redHeightCon]; } @end

2.storyboard实现:

效果:

 

AutoLayout自动布局,NSLayoutConstraint 视图约束使用的更多相关文章

  1. AutoLayout(自动布局)

    1. iOS两种自适应布局方式:(修正说明:) -AutoLayout(自动布局) + SizeClasses(尺寸类别) -Autoresizing (自动调整尺寸/弹簧式调整尺寸) 前者 Auto ...

  2. iOS:Autolayout自动布局实例

    Autolayout自动布局实例即可以用故事板布局,也可以用纯代码创建,各有各的优点:用故事板布局,比较方便,而且直观,可以很直白的看到视图布局后的变化:采用代码布局,虽然代码比较多,有些麻烦,但是可 ...

  3. iOS AutoLayout自动布局&Masonry介绍与使用实践

    Masonry介绍与使用实践:快速上手Autolayout http://www.cnblogs.com/xiaofeixiang/p/5127825.html http://www.cocoachi ...

  4. Visual Format Language(VFL)视图约束

    约束(Constraint)在IOS编程中非常重要,这关乎到用户的直接体验问题. IOS中视图约束有几种方式,常见的是在IB中通过Pin的方式手动添加约束,菜单Editor->Pin->. ...

  5. 实现一个在autolayout下有宽度约束后,自动确定高度的view

    我曾经遇到过一个问题:需要实现一个自定义的label(类似于UILabel),同时需要兼顾UILabel的大小自适应的特性.这个label通常宽度是固定的,通过autolayout指定其宽度约束,但不 ...

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

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

  7. 几种AutoLayout自动布局所经常使用的布局约束类型

    width表示约束ui控件的固定宽度 height表示约束ui控件的固定高度 Leading Space to Superview 与父视图的左边界线保持固定距离 Trailing Space to ...

  8. iOS原生自动布局NSLayoutConstraint

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

  9. AutoLayout自动布局

      原文转自http://www.cnblogs.com/xjf125/p/4895978.html 目录: 一.什么是AutoLayout? 二.创建autoLayout的方法 三.VFL语言   ...

随机推荐

  1. SEO优化---10分钟学会建立高转化率的网站关键词库

    想要优化好一个网站,行业的分析,以及关键词的挖掘是必要的,有一定的关键词排名了,但是转化率和流量方面却很不理想这种情况大部分是只注重了有指数的关键词排名,而忽略了长尾关键词和一些没有指数但是可以带来巨 ...

  2. python 递归函数和二分查找

    1.初始递归 递归属于函数中的一种特殊函数,功能迅速并且干净利落,在函数中递归的基本就是在函数中调用自己本身 def func(): print(111) func() func()#将会无限循环‘1 ...

  3. bzoj 2555 SubString——后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...

  4. MinGW安装与环境变量配置和Sublime Text 2搭建C++编译环境

    MinGW安装与环境变量配置 从http://sourceforge.net/projects/mingw/下载MinGW,安装到D:\MinGW.工具集选择安装(之后还可以进行安装卸载):至少需要安 ...

  5. AngularJS:教程

    ylbtech-AngularJS:教程 1.返回顶部 1. AngularJS 教程 AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(S ...

  6. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  7. Nginx启动提示找不到libpcre.so.1解决方法

    如果是32位系统 [root@lee ~]#  ln -s /usr/local/lib/libpcre.so.1 /lib 如果是64位系统 [root@lee ~]#  ln -s /usr/lo ...

  8. 主流ETL工具

    主流ETL产品: Ascential公司的Datastage(Datastage在2005年被IBM收购).Informatica公司的Powercenter. NCR Teradata公司的ETL ...

  9. leetcode893

    class Solution { public: int numSpecialEquivGroups(vector<string>& A) { set<string> ...

  10. NetCore Selfhost,IIShost,Windows Service Host详解(自宿主、宿主在IIS,宿主在Windows Service中)

    第一部分.自托管 一.依赖.Net Core环境 修改 project.json 文件内容,增加发布时需要包含文件的配置内容(NetCore2.0版本不需要任何设置,NetCore2.0开始彻底放弃p ...