一.方法

 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. dockerfile初试之tomcat8封装

    前面学习了一些docker相关资料,有看到dockerfile这个东东,一时没看得太明白,理论给合实践是最好的学习方法,自己做一下就行了嘛.主要步聚记录如下: 0)环境 10.202.105.96   ...

  2. 分布式缓存系统 Memcached 内存管理机制

    在前面slab数据存储部分分析了Memecached中记录数据的具体存储机制,从中可以看到所采用的内存管理机制——slab内存管理,这也正是linux所采用的内存高效管理机制,对于Memchached ...

  3. XAMPP配置8080端口

    IIS需要HTTP服务,这个服务占用了80端口. Apache启动不了,为了都可以使用,将Apache端口改为8080.

  4. Excel开发学习笔记:根据工作表worksheet内容控制按钮的状态

    开发环境基于VSTO,具体配置:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 在Ribbon工具栏中有2个功能按钮,它们是否可用取决于workshe ...

  5. 执行make出现“Warning: File `xxx.c' has modification time 2.6e+04 s in the future“警告的解决方法

    错误描述: 执行make命令时出现"make[2]: Warning: File `xxx.c' has modification time 1.6e+05 s in the future ...

  6. Angular2快速入门-3.多个组件(分离新闻列表页和详细页)

    上篇(Angular2快速入门-2.创建一个新闻列表)已经完成新闻列表的展示,并且点击新闻列表的时候,下面可以展示出新闻的详细信息,这节我们把新闻详细和新闻列表页面分离出来 新闻详细单独一个compo ...

  7. python's twenty-fifth day for me 模块

    模块: py文件就是模块. python之所以好用,因为模块多. 内置模块:python安装的时候自带的. 扩展模块:别人写好的,需要安装后可直接使用. 自定义模块:自己写的模块. 序列化模块: 能存 ...

  8. 浅层神经网络 反向传播推导:MSE softmax

    基础:逻辑回归 Logistic 回归模型的参数估计为什么不能采用最小二乘法? logistic回归模型的参数估计问题不能“方便地”定义“误差”或者“残差”. 对单个样本: 第i层的权重W[i]维度的 ...

  9. Rhel6.0部署Oracle10g报错相关问题记录

    1.libawt.so及libXt.so.6,lib*系列报错,到最后的界面ruiy哥我笑了!.相信你也会笑的!终于找到根源了!哇哈哈!希望后面不要报error in invoking target ...

  10. 如何使用Visual Studio2015进行C++项目创建

    打开Visual Studio 2015,点击程序左上角的“文件”--“新建”--“项目”. 在弹出窗口选择模板“visual C++”,在新建项目栏里选择“win32控制台应用程序”,直接点击确定. ...