初识 AutoLayout
一、使用"公式":
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的更多相关文章
- 《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 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...
- 初识Hadoop
第一部分: 初识Hadoop 一. 谁说大象不能跳舞 业务数据越来越多,用关系型数据库来存储和处理数据越来越感觉吃力,一个查询或者一个导出,要执行很长 ...
- python学习笔记(基础四:模块初识、pyc和PyCodeObject是什么)
一.模块初识(一) 模块,也叫库.库有标准库第三方库. 注意事项:文件名不能和导入的模块名相同 1. sys模块 import sys print(sys.path) #打印环境变量 print(sy ...
- 初识IOS,Label控件的应用。
初识IOS,Label控件的应用. // // ViewController.m // Gua.test // // Created by 郭美男 on 16/5/31. // Copyright © ...
- UI篇(初识君面)
我们的APP要想吸引用户,就要把UI(脸蛋)搞漂亮一点.毕竟好的外貌是增进人际关系的第一步,我们程序员看到一个APP时,第一眼就是看这个软件的功能,不去关心界面是否漂亮,看到好的程序会说"我 ...
- Python导出Excel为Lua/Json/Xml实例教程(一):初识Python
Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...
- 初识SpringMvc
初识SpringMvc springMvc简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 s ...
随机推荐
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- android SoundPool播放音效
MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同一时候播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音 ...
- HDU 5522 Numbers 暴力
Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...
- Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面
K - Cross SpiderTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/v ...
- CreateProcess的使用方法
使用编译器vs2008. 第一.第二个參数的使用方法: 样例: 使用ie打开指定的网页. 注意第二个參数是 可运行文件+命令行參数 #include "stdafx.h" #inc ...
- 迷途指针 new delete
编程中有一种很难发现的错误是迷途指针.迷途指针也叫悬浮指针.失控指针,是党对一个指针进行delete操作后——这样会释放它所指向的内存——并没有把它设置为空时产生的.而后,如果你没有重新赋值就试图再次 ...
- 小白日记3:kali渗透测试之被动信息收集(二)-dig、whios、dnsenum、fierce
一.DIG linux下查询域名解析有两种选择,nslookup或者dig.Dig(Domain Information Groper)是一个在类Unix命令行模式下查询DNS包括NS记录,A记录,M ...
- Peter's Hobby
Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf humi ...
- The Famous Clock
描述 Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012’s ACM-ICPC World Finals Contest. T ...
- 前端必会css整理
1.设置css样式的三种方式? 外部样式表,引入一个外部css文件 内部样式表,将css代码放在<head>标签内部 内联样式,将css样式 ...