一、使用"公式":

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. java foreach编辑讲解

    foreach语句使用总结 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreac ...

  2. android 自定义按钮实现 home键 和返回键

    由于在自己做的东西中用到了就总结一下,自己做了测试 在一个程序运行中如果按 返回键  分别执行了 : onpause()     onStop()   onDestory()方法 如果点击 home键 ...

  3. 使用导入导出进行备份和恢复OCR(10g)

    Oracle推荐在对集群调整时,比方添加.删除节点之前,应对OCR进行备份,能够用export备份到指定文件.假设做了replace或者restore等操作,Oracle建议使用cluvfy comp ...

  4. PAT 1002

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  5. 随机森林实现 MATLAB

    matlab 中随机森林工具箱的下载地址: http://code.google.com/p/randomforest-matlab/downloads/detail?name=Windows-Pre ...

  6. Helpers\Hooks

    Helpers\Hooks Add modules with hooks The hooks helper allows modules to be created within the module ...

  7. 小白日记31:kali渗透测试之Web渗透-扫描工具-Arachni

    扫描工具-Arachni Kali中集成旧的arachni的阉割版,所以需要重新安装[在某些方面有其独特性,但不算很强大,有命令行和web两种使用方式][匿名者推荐] apt-get update h ...

  8. 大文件读取方法(C#)

    之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...

  9. C笔记01:关于printf函数输出先后顺序的讲解

    关于printf函数输出先后顺序的讲解!! 对于printf函数printf("%d%d\n", a, b);函数的实际输出顺序是这样的先计算出b,然后再计算a,接着输出a,最后再 ...

  10. Android(java)学习笔记99:android的短信发送器研究

    1.第一种可以调用系统内部的短信程序. 之前我曾经出现过一个bug就是报错: android.content.ActivityNotFoundException: No Activity found ...