一、使用"公式":

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. PostgreSQL的initdb 源代码分析之六

    继续分析 下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形. effective_user = get_id(); ) username = effective_ ...

  2. C++11新特性(3) lambda表达式(1)

    C++11加入了一项名为lambda表达式的新功能.通过这项功能能编写内嵌的匿名函数,而不必编写独立函数或函数对象,使得代码更加理解. lambda表达式包括下面部分. [capture_block] ...

  3. Java模拟登陆新浪微博抓取数据【转载】

    package com.shiyimm.crawler.weibo; import java.io.FileNotFoundException; import java.io.FileReader; ...

  4. material-singleinputform

    https://github.com/HeinrichReimer/material-singleinputform https://play.google.com/store/apps/detail ...

  5. cookie转CookieCollection

    CookieCollection cookiesResponse = new CookieCollection(); if (response != null) { foreach (string c ...

  6. class、classLoader的getResourceAsStream的区别

    1.class.getResourceAsStream() 从源码中可以看出他也是调用ClassLoader的getResourceAsStream() public InputStream getR ...

  7. iOS开发——网络编程Swift篇&(五)同步Post方式

    同步Post方式 // MARK: - 同步Post方式 func synchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: &qu ...

  8. LVS三种模式配置及优点缺点比较 转

    LVS三种模式配置及优点缺点比较   作者:gzh0222,发布于2012-11-12,来源:CSDN   目录: LVS三种模式配置 LVS 三种工作模式的优缺点比较 LVS三种模式配置 LVS三种 ...

  9. 关于MIUI悬浮窗权限问题的解决方案

    先扯会....好久没写Blog了....这段时间有点小忙...瞎忙.....忙的自己都感觉都不应该.....严重影响了生活质量......生活的幸福指数!!!.....到现在还特么的单身!!!求介绍啊 ...

  10. Spark之路 --- Scala用JFreeChart画图表实例

    JFreeChart介绍 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用 ...