一、使用"公式":

1、frame: 原点以及自身的位置来确定自身的位置

2、autoLayout: 根据参照视图的位置  来定义自己的位置

3、autoLayout: 相对布局  约束视图和视图之间的关系 来分配 屏幕上的位置

4、使用VFL(Visual Format Language 视觉格式化语言)通过添加字符,来约束视图和视图之间的关系

5、使用autoLayout 必须把 translatesAutoresizingMaskIntoConstraints禁用才可以使用相对布局是找一个参照物 拿参照物当做基础,设置他和参照物的相对距离 来设置自己的位置

6、FVL 需有 横 竖 两个方向的约束  ( 1、“H:”(横向),

2、“V:”(竖向),

3、“|” (表示它的父视图),

4、“-50-”(表示两个视图之间的间距),

5、“[textField]”)

7、H:横向

| 表示他的父视图

-50- 表示后面视图 与前面视图的距离 (后面视图是textField,前面视图是他的父视图)

[textField(>=200)] 要约束视图的宽  (>=200)允许最小的宽度是200  如果是竖向  就是允许最小的高度

@"H:|-50-[textField(>=200)]-50-|"

距离坐边原点距离50   右边边界距离50    允许视图的最小宽度是200

8、使用 autoLayout 适配的时候 以最小尺寸设备 为基准

二、使用示例:

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad];
[self demo3]; } // 一个视图
- (void)demo1 {
UIView *view = [[UIView alloc] init];
// 如果使用autoLayout 那么 禁止使用 translatesAutoresizingMaskIntoConstraints 属性 view.translatesAutoresizingMaskIntoConstraints = NO;
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view]; // VFL 横向 竖向布局
// @"H:" 设置横向布局
// @"H:|-20-"设置横向布局 距离父视图的左侧边距
// @"H:|-20-[view(>=200)]" 设置横向布局 距离父视图的左侧边距, 设置view横向的 尺寸不能小于200
// @"H:|-20-[view(>=200)]-20-|" 设置横向布局 距离父视图的左侧边距, 设置view横向的 尺寸不能小于200 设置右侧与父视图之间的间距 // @"V:|-40-[view(>=200)]-20-|"设置竖向布局 距离顶部边距40,设置view的尺寸不能小于400,设置底部与父视图之间的边距为20
// 使用VFL 需要把视图的对象(视图)与 他的名字(字符串)绑定起来
NSDictionary *views = NSDictionaryOfVariableBindings(view);
// 给 self.view 和 view 添加约束
// addConstraints 添加约束的 方法
// NSLayoutConstraint 添加具体约束的一个类 // + (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
// format VFL;
// opts 同意按照某个方向去布局;
// metrics 绑定的参数;
// views 绑定的视图参数
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view(>=200)]-20-|" options: metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view(>=200)]-20-|" options: metrics:nil views:views]]; } - (void)demo2 {
UIView *view = [[UIView alloc] init];
// 如果使用autoLayout 那么 禁止使用 translatesAutoresizingMaskIntoConstraints 属性 view.translatesAutoresizingMaskIntoConstraints = NO;
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view]; UIView *view1 = [[UIView alloc] init];
// 如果使用autoLayout 那么 禁止使用 translatesAutoresizingMaskIntoConstraints 属性 view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor brownColor];
[self.view addSubview:view1]; NSDictionary *views = NSDictionaryOfVariableBindings(view,view1);
// 红色view 的横向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view(>=200)]-20-|" options: metrics:nil views:views]];
// 红色view 的竖向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view(50)]-10-[view1]" options: metrics:nil views:views]]; // 棕色view1 的横向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view1(>=200)]-20-|" options: metrics:nil views:views]];
// 棕色view1 的竖向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-10-[view1(50)]" options: metrics:nil views:views]]; UIView *view2 = [[UIView alloc] init];
view2.translatesAutoresizingMaskIntoConstraints = NO;
view2.backgroundColor = [UIColor greenColor];
[self.view addSubview:view2];
// view2 横向
// view2 竖向
} // 优化 demo2
- (void)demo3 {
UIView *view = [[UIView alloc] init];
// 如果使用autoLayout 那么 禁止使用 translatesAutoresizingMaskIntoConstraints 属性 view.translatesAutoresizingMaskIntoConstraints = NO;
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view]; UIView *view1 = [[UIView alloc] init];
// 如果使用autoLayout 那么 禁止使用 translatesAutoresizingMaskIntoConstraints 属性 view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor brownColor];
[self.view addSubview:view1]; NSDictionary *views = NSDictionaryOfVariableBindings(view,view1);
// 红色view 的横向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view(>=200)]-20-|" options: metrics:nil views:views]];
// 红色view 棕色view1 的竖向约束
// [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view(50)]-10-[view1(50)]" options:0 metrics:nil views:views]]; // 红色view 棕色view1 两个视图的高度 都是50
// [view1(view)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view(50)]-10-[view1(view)]" options: metrics:nil views:views]]; // 棕色view1 的横向约束
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view1(>=200)]-20-|" options: metrics:nil views:views]]; // // 棕色view1 的竖向约束
// [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-10-[view1(50)]" options:0 metrics:nil views:views]]; }

三、模拟器运行效果图

demo1 运行效果:

demo2 运行效果:

demo3 运行效果:

初识 AutoLayout的更多相关文章

  1. 《iOS开发实战 从入门到上架App Store(第2版)》书籍目录

    第1章 开发准备 1.1 iOS 10新特性简述 1.1.1 新增触觉反馈编程接口 1.1.2 SiriKit框架的开放 1.1.3 引入Messages App 1.1.4 通知框架的整合与扩展 1 ...

  2. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  3. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  4. 初识Hadoop

    第一部分:              初识Hadoop 一.             谁说大象不能跳舞 业务数据越来越多,用关系型数据库来存储和处理数据越来越感觉吃力,一个查询或者一个导出,要执行很长 ...

  5. python学习笔记(基础四:模块初识、pyc和PyCodeObject是什么)

    一.模块初识(一) 模块,也叫库.库有标准库第三方库. 注意事项:文件名不能和导入的模块名相同 1. sys模块 import sys print(sys.path) #打印环境变量 print(sy ...

  6. 初识IOS,Label控件的应用。

    初识IOS,Label控件的应用. // // ViewController.m // Gua.test // // Created by 郭美男 on 16/5/31. // Copyright © ...

  7. UI篇(初识君面)

    我们的APP要想吸引用户,就要把UI(脸蛋)搞漂亮一点.毕竟好的外貌是增进人际关系的第一步,我们程序员看到一个APP时,第一眼就是看这个软件的功能,不去关心界面是否漂亮,看到好的程序会说"我 ...

  8. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  9. 初识SpringMvc

    初识SpringMvc springMvc简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 s ...

随机推荐

  1. webservice 缓存机制

    本文转载:http://blog.csdn.net/zhdd1234/article/details/4555472 WebService的缓存分为两种,一种是简单的输出缓存,一种是强大的数据缓存 一 ...

  2. OAuth 2.0

    国外 OAuth2.0 大全:http://oauth.net/2/ 国内经典:http://www.fising.cn/2011/03/%E4%B8%80%E6%AD%A5%E4%B8%80%E6% ...

  3. 寻ta分析与站点内容

    从 寻ta 突然来的訪问量就開始在想.站点内容是否才是真正须要的东西. 寻ta分析 作为一篇文章带来的影响,我们能够看看訪问会话. 日期 訪问量 5.5 9 5.6 4618 5.7 1216 5.8 ...

  4. JAVA实现HTTPserver端

    用java socket实现了一个简单的httpserver, 能够处理GET, POST,以及带一个附件的multipart类型的POST.尽管中途遇到了非常多问题, 只是通过在论坛和几个高手交流了 ...

  5. Don't Repeat Yourself (不要重复你自己)

    DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性.简洁性,并且可能导致代码之间的矛盾.<The Pragmatic Programm ...

  6. BZOJ 1968: [Ahoi2005]COMMON 约数研究 水题

    1968: [Ahoi2005]COMMON 约数研究 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeO ...

  7. HttpClient 设置代理方式

    HttpClient httpClient = new HttpClient(); //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setPro ...

  8. 使用DataSet Datatable 更新数据库的三种方式

    1:自动生成命令的条件 CommandBuilder 方法a)动态指定 SelectCommand 属性b)利用 CommandBuilder 对象自动生成 DataAdapter 的 DeleteC ...

  9. HDU 4293 Groups (线性dp)

    OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...

  10. 使用sql生成UUID

    在SQLServer中使用该sql语句可以生成GUID:select cast(NEWID() as varchar(36)) as uuid 通过一下语句将GUID中的'-'字符去掉: select ...