AutoLayout是从IOS 6开始苹果引入来取代autoresizing的新的布局技术,该技术有三种设置方式,等下我来为大家一一叙述一下。

在说三种设置方式前,我们先简单的说一下autolayout能够设置哪些行为。

1.视图的大小(即视图的绝对大小)。

2.视图的位置(视图相对于父视图或者兄弟视图的位置)。

3.视图的对齐方式(相对于父视图或者相对于兄弟视图)。

  可以看到autolayout相比autoresizing技术来说要灵活的多,该技术有很多布局的约束设置。这次主要讲的用代码来设置AutoLayout,代码向我们需要添加autoLayout视图使用该方法

+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

该方法实际上就是满足一个数学关系

item1 =(>=,<=) multiplier * item2 + constant。

参数说明:

view1:第一个视图即item1。

attr1:是第一个视图选择的属性

relation:即中间的关系(= , >= , <=)

view2:第二个视图即item2。

attr2:是第二个视图选择的属性

c:就是常熟constant。

  举个简单的例子来说我们想设置第一个视图的宽度是第二个视图宽度的2倍,我们可以这样写:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:2 constant:0]];

可以看到这里我们item1是view1,item2是view2,attr1是attribute:NSLayoutAttributeWidth,attr2是attribute:NSLayoutAttributeWidth,relation是NSLayoutRelationEqual,mutiplier 是2,constant是0.

带入上面的公式得:

第一个视图(宽度) = 2 * 第二个视图(宽度) + 0

如下是我们所有可以控制的属性:

NSLayoutAttributeLeft 视图的左边
NSLayoutAttributeRight 视图的右边
NSLayoutAttributeTop 视图的上边
NSLayoutAttributeBottom 视图的下边
NSLayoutAttributeLeading 视图的前边
NSLayoutAttributeTrailing 视图的后边
NSLayoutAttributeWidth 视图的宽度
NSLayoutAttributeHeight 视图的高度
NSLayoutAttributeCenterX 视图的中点的X值
NSLayoutAttributeCenterY 视图中点的Y值
NSLayoutAttributeBaseline 视图的基准线
NSLayoutAttributeNotAnAttribute 无属性

  

这里解释一下前边NSLayoutAttributeLeading和后边NSLayoutAttributeTrailing,这里前边和后边并不是总是为左边和右边的,有些国家的前边是右边后边是左边所以这样设定是为了国际化考虑。还有视图基准线NSLayoutAttributeBaseline通常是指视图的底部放文字的地方。

  接下我们一起来看一个demo

  我们想让两个视图Y方向居中,第一个视图距离左边缘20,第一个视图以第二个视图等大并且X方向距离为100。

 UIView *view1 = [[UIView alloc] init];
UIView *view2 = [[UIView alloc] init];
[self.view addSubview:view1];
[self.view addSubview:view2];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view2.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor blueColor];
view2.backgroundColor = [UIColor grayColor];
//set view1 height and width
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier: constant:]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier: constant:]];
//set view2 height and width
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier: constant:]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier: constant:]];
//set relationship between view1 and view2
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier: constant:]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier: constant:]];
//set relationship between topView and view1
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier: constant:]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier: constant:]];

下面我们一起来看一下这段代码

注意 5、6行设置view的 translatesAutoresizingMaskIntoConstraints 属性为NO,意思就是遵循autoLayout抛弃原有设置的高度宽度等,使用autolayout的视图必须要设置该属性。

10、11行设置view1的宽和高,大家可能已经发现item2为nil并且attrbute为attribute:NSLayoutAttributeNotAnAttribute,这样做我们带入公式就会明白

item1 = m * 0 + constant。也就是直接设置本视图的宽和高。

13、14行是设置view2的宽高和view1相同,这里细心的同学可能会发现添加约束的对象并不是像上面设置宽高时的view1,而是它们共同的父视图self.view。因为在autolayout中有这样的规定,如果是一元约束,即只针对自己的约束,那么就直接添加在该视图上。如果是二元约束,那么就必须要添加在它们的共同最近的父视图上面。

15、16行是设置view1和view2的关系,设置view1和view2具有相同的Y,并且view2在view1右边距离100的位置。

18、19行最后我们设置了view1左边距离父视图左边20的距离,并且view1的Y等于父视图Y的中点值。

通过以上的设置,我们运行的结果就是:

如图,视图1在距左边20的位置,视图1视图2都Y方向居中并且相距100的距离。

删除约束:

  removeConstraint:(NSLayoutConstraint *) constraint;

在删除约束时要找准约束的绑定对象。

AutoLayout的三种设置方式之——NSLayoutConstraint代码篇的更多相关文章

  1. 通过三个DEMO学会SignalR的三种实现方式

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...

  2. Qt 2D绘图 渐变填充(三种渐变方式)

    在qt中提供了三种渐变方式,分别是线性渐变,圆形渐变和圆锥渐变.如果能熟练应用它们,就能设计出炫目的填充效果. 线性渐变: 1.更改函数如下: void Dialog::paintEvent(QPai ...

  3. Linux 双线策略路由的三种实现方式总结+端口映射

    Linux 双线策略路由的三种实现方式总结+端口映射 Linux 双线策略路由的三种实现方式总结+端口映射 网络环境 服务器(网关): eth0 为LAN口,IP为 LAN_IP = 192.168. ...

  4. linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用

    ---操作环境--- 虚拟机版本:VMware Workstation_10.0.3 Linux系统版本:CentOS_6.5(64位) 物理机系统版本:win10  一.虚拟网络三种连接方式 当在V ...

  5. Spring IOC 三种注入方式

    1.    接口注入 2.    setter注入 3.    构造器注入 对象与对象之间的关系可以简单的理解为对象之间的依赖关系:A类需要B类的一个实例来进行某些操作,比如在A类的方法中需要调用B类 ...

  6. 垃圾回收(GC)的三种基本方式

    垃圾(Garbage)就是程序需要回收的对象,如果一个对象不在被直接或间接地引用,那么这个对象就成为了「垃圾」,它占用的内存需要及时地释放,否则就会引起「内存泄露」.有些语言需要程序员来手动释放内存( ...

  7. 瀑布流的三种实现方式(原生js+jquery+css3)

    前言 项目需求要弄个瀑布流的页面,用的是waterfall这个插件,感觉还是可以的,项目赶就没自己的动手写.最近闲来没事,就自己写个.大致思路理清楚,还是挺好实现的... 原生javascript版 ...

  8. VMware三种链接方式

    VMware三种链接方式 第一种:桥接Bridged 如其的说明:connected directly to the physical networkà直接连接到物理网络.如果是通过路由器连接出来的D ...

  9. Service组件 总结 + 绑定理Service三种实现方式 Messager + Binder + AIDL

    在Android中进程按优先级可以分为五类,优先级从高到低排列: - 前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity - 可视进程 该进程中的组件虽然没有和用户交互,但是仍 ...

随机推荐

  1. Swift 自己主动引用计数机制ARC

    Swift 使用自己主动引用计数(ARC)这一机制来跟踪和管理你的应用程序的内存.通常情况下,Swift 的内存管理机制会一直起着作用,你无须自己来考虑内存的管理.ARC 会在类的实例不再被使用时,自 ...

  2. MySQL · 引擎特性 · InnoDB 事务锁简介

    https://yq.aliyun.com/articles/4270# zhaiwx_yinfeng 2016-02-02 19:00:43 浏览2194 评论0 mysql innodb lock ...

  3. linux下的十六进制编辑器---wxHexEdit

    ....其实wxHexEdit是一个跨平台的十六进制编辑器,支持windows,linux,mac. 之所以标题用linux...是因为windows下多数都用winhex,UE之类的编辑器,而lin ...

  4. mysql基本定义--数据类型

    浮点数类型与定点数类型: MySQL中使用浮点数类型和定点数类型来表示小数. 浮点数类型包括单精度浮点数(float型)和双精度浮点数(double型).定点数类型就是decimal型. OK,现在我 ...

  5. [Android开发]- MVC的架构实现登录模块-1

    本系列博客主要展示一下,在C-S(Client - Server)系统开发当中,如何使用MVC的架构来实现安卓端的一个登录验证的模块.如果你能有基本的数据库开发,WEB开发,和安卓开发的知识,那么理解 ...

  6. C# 数据的加密解密

    /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param& ...

  7. nodejs的mysql模块学习(八)关闭连接池

    关闭连接池 可以用pool.end()关闭连接池 pool.end(function (err) { // 所有的连接都已经被关闭 }); 当关闭之后pool将不可以getconnection()

  8. label

    label的使用 以前只知道使用并没太注意一些细节 话说<label><input type="checkbox"/>5星</label>就可以 ...

  9. 明风:分布式图计算的平台Spark GraphX 在淘宝的实践

    快刀初试:Spark GraphX在淘宝的实践 作者:明风 (本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) ...

  10. 【阿里云产品评测】小站长眼中的巅峰云PK

    [阿里云产品评测]小站长眼中的巅峰云PK 阿里云论坛用户:昵称-a5lianmeng 笔者是一名小站长,因狂热互联网,而在毕业后由宅男逐渐进入站长队伍,在毕业后的几年间,经营6个流量类网站,身为站长, ...