使用AutoLayout之前需要知道以下两点:

1.必须设置 translatesAutoresizingMaskIntoConstraints为NO。

2.如果是viewControl则AutoLayout适配写在[- updateViewConstraints]中;

如果是view则AutoLayout适配写在[- updateConstraints]中。

一、要讲解的方法:

 /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
*/
+(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;

参数说明:

第一个参数 view1: 要设置的视图;

第二个参数 attr1: view1要设置的属性,稍后详解;

第三个参数 relation: 视图view1和view2的指定属性之间的关系,稍后详解;

第四个参数 view2: 参照的视图;

第五个参数 attr2: 参照视图view2的属性,稍后详解;

第六个参数 multiplier: 视图view1的指定属性是参照视图view2制定属性的多少倍;

第七个参数 c: 视图view1的指定属性需要加的浮点数。

根据参数的讲解,得出计算公式如下:

view1.attr1 [= , >= , <=] view2.attr2 * multiplier + c;

参数详解:

1、NSLayoutAttribute

 typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = ,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeNotAnAttribute =
};

分三部分解释 NSLayoutAttribute

第一部分:常用的

NSLayoutAttributeLeft: CGRectGetMinX(view.frame);

NSLayoutAttributeRight: CGRectGetMaxX(view.frame);

NSLayoutAttributeTop: CGRectGetMinY(view.frame);

NSLayoutAttributeBottom: CGRectGetMinY(view.frame);

NSLayoutAttributeWidth: CGRectGetWidth(view.frame);

NSLayoutAttributeHeight: CGRectGetHeight(view.frame);

NSLayoutAttributeCenterX: view.center.x;

NSLayoutAttributeCenterY:view.center.y ;

NSLayoutAttributeBaseline: 文本底标线,在大多数视图中等同于NSLayoutAttributeBottom; 在少数视图,如UILabel,是指字母的底部出现的位置;

NSLayoutAttributeLastBaseline: 相当于NSLayoutAttributeBaseline;

NSLayoutAttributeFirstBaseline: 文本上标线;

NSLayoutAttributeNotAnAttribute: None;

第二部分: 根据国家使用习惯不同表示的意思不同

NSLayoutAttributeLeading: 在习惯由左向右看的地区,相当于NSLayoutAttributeLeft;在习惯从右至左看的地区,相当于NSLayoutAttributeRight;

NSLayoutAttributeTrailing: 在习惯由左向右看的地区,相当于NSLayoutAttributeRight;在习惯从右至左看的地区,相当于NSLayoutAttributeLeft;

第三部分:ios8新增属性,各种间距,具体用法下节介绍

NSLayoutAttributeLeftMargin,

NSLayoutAttributeRightMargin,

NSLayoutAttributeTopMargin,

NSLayoutAttributeBottomMargin,

NSLayoutAttributeLeadingMargin,

NSLayoutAttributeTrailingMargin,

NSLayoutAttributeCenterXWithinMargins,

NSLayoutAttributeCenterYWithinMargins,

从网上找了一张图,标注以上属性

2、NSLayoutRelation

 typedef NS_ENUM(NSInteger, NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual = -,
NSLayoutRelationEqual = ,
NSLayoutRelationGreaterThanOrEqual = ,
};

NSLayoutRelationLessThanOrEqual: <=;

NSLayoutRelationEqual: =;

NSLayoutRelationGreaterThanOrEqual: >=;

二、要讲解的方法

1、获取当前view中所有的 NSLayoutConstraint

 - (NSArray *)constraints NS_AVAILABLE_IOS(6_0);

2、旧版方法,将指定的NSLayoutConstraint添加到页面或者从页面中移除

  - (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.  Instead, set NSLayoutConstraint's active property to YES.
- (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint activateConstraints:].
- (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead set NSLayoutConstraint's active property to NO.
- (void)removeConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint deactivateConstraints:].

3、ios8新加方法,激活或者停用指定约束

 /* The receiver may be activated or deactivated by manipulating this property.  Only active constraints affect the calculated layout.  Attempting to activate a constraint whose items have no common ancestor will cause an exception to be thrown.  Defaults to NO for newly created constraints. */
@property (getter=isActive) BOOL active NS_AVAILABLE(10_10, 8_0); /* Convenience method that activates each constraint in the contained array, in the same manner as setting active=YES. This is often more efficient than activating each constraint individually. */
+ (void)activateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0); /* Convenience method that deactivates each constraint in the contained array, in the same manner as setting active=NO. This is often more efficient than deactivating each constraint individually. */
+ (void)deactivateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);

三、Coding Time

a> 设置视图view1为 宽度=20的正方形

两种写法,第一种 宽度=20,高度=20

     [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:]];

第二种 宽度=20, 高度=宽度

     [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:]];

第二种方法的优势是,如果想修改view1的大小,只需要修改一处。

b>设置视图view1.frame.origin.x = 视图view2.frame.origin.x

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:];

//旧版方法
//[self addConstraint:leftConstraint]; //新版方法1
[NSLayoutConstraint activateConstraints:@[leftConstraint]];
//新版方法2
leftConstraint.active = YES;

IOS页面自动布局 之 NSLayoutConstraint基础篇的更多相关文章

  1. iOS开发swift语法0基础篇—————(swift技术交流群:361513739)

    iOS开发之swift语法0基础篇:点击打开链接  swift技术交流QQ群361513739

  2. Hybrid APP基础篇(三)->Hybrid APP之Native和H5页面交互原理

    本文已经不维护,新地址: http://www.cnblogs.com/dailc/p/8097598.html 说明 Hybrid模式原生和H5交互原理 目录 前言 参考来源 前置技术要求 楔子 A ...

  3. iOS系列 基础篇 03 探究应用生命周期

    iOS系列 基础篇 03 探究应用生命周期 目录: 1. 非运行状态 - 应用启动场景 2. 点击Home键 - 应用退出场景 3. 挂起重新运行场景 4. 内存清除 - 应用终止场景 5. 结尾 本 ...

  4. iOS系列 基础篇 04 探究视图生命周期

    iOS系列 基础篇 04 探究视图生命周期 视图是应用的一个重要的组成部份,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 以视图的四种状态为基础,我们来系统了解一下视 ...

  5. iOS系列 基础篇 05 视图鼻祖 - UIView

    iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...

  6. iOS系列 基础篇 06 标签和按钮 (Label & Button)

    iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...

  7. iOS系列 基础篇 07 Action动作和输出口

    iOS系列 基础篇 07 Action动作和输出口 目录:  1. 前言及案例说明 2. 什么是动作? 3. 什么是输出口? 4. 实战 5. 结尾 1. 前言及案例说明 上篇内容我们学习了标签和按钮 ...

  8. iOS系列 基础篇 08 文本与键盘

    iOS系列 基础篇 08 文本与键盘 目录: 1. 扯扯犊子 2. TextField 3. TextView 4. 键盘的打开和关闭 5. 打开/关闭键盘的通知 6. 键盘的种类 7. 最后再扯两句 ...

  9. iOS系列 基础篇 09 开关、滑块和分段控件

    iOS系列 基础篇 09 开关.滑块和分段控件 目录: 案例说明 开关控件Switch 滑块控件Slider 分段控件Segmented Control 1. 案例说明 开关控件(Switch).滑块 ...

随机推荐

  1. ubuntu 16.04下安装oracle jdk 1.7

    网上搜索了下,知道了大概,不能用apt装了,oracle也不再提供deb包了.只能下tar.gz包自己装. 先下载下来jdk:http://www.oracle.com/technetwork/jav ...

  2. jQuery中position()与offset()区别

    使用jQuery获取元素位置时,我们会使用position()或offset()方法,两个方法都返回一个包含两个属性的对象-左边距和上边距,它们两个的不同点在于位置的相对点不同. 可以看看下边的图: ...

  3. AndroidStudio小技巧--依赖库

    同步发表于http://avenwu.net/2015/02/12/androidstudio_library_dependency Fork on github https://github.com ...

  4. 深入剖析 redis 事件驱动

    概述 redis 内部有一个小型的事件驱动,它和 libevent 网络库的事件驱动一样,都是依托 I/O 多路复用技术支撑起来的. 利用 I/O 多路复用技术,监听感兴趣的文件 I/O 事件,例如读 ...

  5. SymmetricDS 数据库双向同步开源软件入门

    一句话概括该软件:SymmetricDS是一个文件和数据库同步软件,开源的,支持多主复制,同步时过滤和在异构的网络环境中进行数据转换传输.它支持单向和双向上的多个订阅者,异步的数据复制. 以下是从CS ...

  6. CentOS下Red5安装

    Red5介绍 Red5是一个采用Java开发开源的Flash流媒体服务器.它支持:把音频(MP3)和视频(FLV)转换成播放流: 录制客户端播放流(只支持FLV):共享对象:现场直播流发布:远程调用. ...

  7. VC中使用ATL库实现正则表达式匹配(ADODB::Error)

    1. 确保项目属性中ATL使用处于打开状态. 如VS中项目属性常规—ATL使用—静态链接到ATL 2. 在使用时加上头文件 #include "atlrx.h" 3. 使用示例代码 ...

  8. 飞思卡尔9S12X系列双核中的协处理器XGATE使用方法

    http://adi.chinaaet.com/analog/blogdetail/24482.html

  9. IE11兼容性设定

    增加browser文件,如下:  <browsers>   <browser refID="Default">     <capabilities&g ...

  10. 专访Linux嵌入式开发韦东山操作系统图书作者--转

    CSDN学院讲师韦东山:悦己之作,方能悦人 发表于2015-04-28 08:09| 6669次阅读| 来源CSDN| 24 条评论| 作者夏梦竹 专访Linux嵌入式开发韦东山操作系统图书作者 摘要 ...