一.方法

 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. Oracle GoldenGate理论

    1Oracle GoldenGate处理方法和支持的数据库Oracle GoldenGate在多样和异构的基础IT平台中,可以在事务级别上进行数据交换和数据操作.在保证交易完整性和最小的开销的条件下, ...

  2. 安装用于跨平台移动开发的 Visual C++

    安装用于跨平台移动开发的 Visual C++ Visual Studio 2015   若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 20 ...

  3. selenium之 chromedriver与chrome版本映射表

    看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.40 v66-68 v2. ...

  4. 分布式缓存系统 Memcached slab和item的主要操作

    上节在分析slab内存管理机制时分析Memcached整个Item存储系统的初始化过程slabs_init()函数:分配slabclass数组空间,到最后将各slab划分为各种级别大小的空闲item并 ...

  5. Annotation之三:自定义注解示例,利用反射进行解析

    @Retention定义了该Annotation被保留的时间长短有3中RetentionPolicy.SOURCE源文件有效,RetentionPolicy.CLASS:在class文件中有效,Ret ...

  6. 【转】分布式存储和一致性hash

    本文我将对一致性算法作介绍,同时谈谈自己对一致性hash和一般意义上的hash算法的区别 hash是什么 hash即hash算法,又称为散列算法,百度百科的定义是 哈希算法将任意长度的二进制值映射为较 ...

  7. 委托BegionInvoke和窗体BegionInvoke

    委托BegionInvoke是指通过委托方法执行多线程任务,例如: //定义委托成员变量 delegate  void dg_DeleAirport(); //指定委托函数 dg_DeleAirpor ...

  8. Python 编码机制

    python 编码转换 Python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动 ...

  9. Python技巧(一)

    一  if..else的多种写法 a, b, c = 1, 2, 3 1.常规 if a > b: c = a else: c = b 2.表达式 c = a if a > b else ...

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

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