//
// ViewController.m
// IOS_0115_AutoLayout
//
// Created by ma c on 16/1/15.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSLayoutConstraint *changeBlueTop;
@property (nonatomic, strong) UIView *blueView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //创建蓝色View
UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(, , , );
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
self.blueView = blueView;
//创建红色View
UIView *redView = [[UIView alloc] init];
redView.frame = CGRectMake(, , , );
redView.backgroundColor = [UIColor redColor];
[blueView addSubview:redView]; //禁用autoresizing
blueView.translatesAutoresizingMaskIntoConstraints = NO;
redView.translatesAutoresizingMaskIntoConstraints = NO; //创建并添加约束 //1.创建蓝色View的约束 /*
(id)对象 的 (NSLayoutAttribute)属性
(NSLayoutRelation)关系(> = <)
(id)对象的(NSLayoutAttribute)属性
乘以multiplier的参数 加上constant的参数
*/ //高度的约束
NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:];
//把约束加到控件上
[blueView addConstraint:blueHeight]; //距离左边30
NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:];
[blueView.superview addConstraint:blueLeft]; //距离上面30(topLayoutGuide状态栏)
NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:];
[blueView.superview addConstraint:blueTop];
self.changeBlueTop = blueTop; //距离右边30
NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-];
[blueView.superview addConstraint:blueRight]; //1.创建红色View的约束
//红色View的高度等于蓝色View的高度
NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
[redView.superview addConstraint:redHeight]; //红色view的Top距离蓝色View的Bottom30
NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:];
[redView.superview addConstraint:redTop]; //红色View与蓝色View右对齐
NSLayoutConstraint *redRight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:];
[redView.superview addConstraint:redRight]; //红色View的宽度等于蓝色View的宽度的一半
NSLayoutConstraint *redWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:];
[redView.superview addConstraint:redWidth]; //修改约束实现动画效果 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(, , , )];
[btn setTitle:@"移动" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; } - (void)btnClick
{
//修改约束-没有根据新的约束计算蓝色View的frame
self.changeBlueTop.constant += ;
[UIView animateWithDuration: animations:^{
//根据新的约束修改新的frame
[self.blueView layoutIfNeeded];
}];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

VFL语言-可视化格式语言(Visual Format Language)

1.代码分析:
1    NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:<#(NSString *)#> options:<#(NSLayoutFormatOptions)#> metrics:<#(NSDictionary *)#> views:<#(NSDictionary *)#>]
VisualFormat:VFL语句
options:约束类型
metrics :VFL语句中用到的具体数值
views :VFL语句中用到的控件
 
2.使用步骤:
创建控件
添加到父控件
禁用Autoresizing
添加约束
 
3.总结
简化了代码量,方便书写约束
不支持乘法
 
//
// ViewController.m
// IOS_0116_VFL
//
// Created by ma c on 16/1/16.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 链式语法
UIView *blueView = UIView.new;
blueView.backgroundColor = UIColor.blueColor;
[self.view addSubview:blueView]; UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; blueView.translatesAutoresizingMaskIntoConstraints = NO;
redView.translatesAutoresizingMaskIntoConstraints = NO; /** 一个方向上的所有控件的对齐方法
NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),
NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),
NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),
NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),
NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),
NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),
NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),
NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),
NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline),
NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline,
NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline),
*/
// 添加约束 // 添加blueView水平方向的约束
NSArray *blueViewH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:@{@"blueView" : blueView}];
[self.view addConstraints:blueViewH]; NSArray *blueAndRedV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView(50)]-20-[redView(blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}];
[self.view addConstraints:blueAndRedV]; // VFL语言不支持运算符
// NSArray *redViewWidth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}];
// [self.view addConstraints:redViewWidth]; NSLayoutConstraint *redViewWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:];
[self.view addConstraint:redViewWidth];
} @end

Masonry轻量级布局框架

meɪs(ə)nrɪ

采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性

 //
// ViewController.m
// IOS_0106_Masonry
//
// Created by ma c on 16/1/16.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
// 如果想在使用Masonry框架时省略mas_前缀请定义下面这个宏
#define MAS_SHORTHAND //define this constant if you want to enable auto-boxing for default syntax
// 如果你想在使用equalTo的时候让它也带有自动装箱功能请定义下面这个宏
#define MAS_SHORTHAND_GLOBALS
#warning mark - 上面的两个宏必须定义在框架的主头文件的上面 #import "Masonry.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView]; // 添加约束
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(self.view.left).offset(50);
make.top.equalTo(self.view.mas_top).offset();
make.right.equalTo(self.view.mas_right).offset(-);
make.bottom.equalTo(self.view.mas_bottom).offset(-); // 当约束控件的属性和参照控件的属性相同时,参数控件的属性可以省略不写
// make.left.equalTo(self.view).offset(50);
// make.top.equalTo(self.view).offset(50);
// make.right.equalTo(self.view).offset(-50);
// make.bottom.equalTo(self.view).offset(-50); // 当两个约束属性的offset值一样的时候属性也可以连写
// make.left.top.equalTo(self.view).offset(50);
// make.right.bottom.equalTo(self.view).offset(-50); // make.edges.equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
// mas_equalTo可以把基本数据类型转换为对象类型,这个转换过程叫装箱,如果把一个对象类型转换成一个基本数据类型,为拆箱,解箱
// make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50)); }]; // 更新约束
// 如果之前已经添加过有相同的属性,在此方法中可以重写添加在此添加时会把之前添加的相同属性的约束覆盖掉
// 如果在此方法中添加的了新属性的约束,可能会照成约束冲突
[blueView updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset();
// make.width.equalTo(200);
}]; // 重置blueView的约束"把blueView之前添加的所有约束删除"同时也可以在此方法中重新给blueView添加新的约束
[blueView remakeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(, , , )); }]; } @end

iOS UI-自动布局(AutoLayout)的更多相关文章

  1. iOS:自动布局Autolayout

    自动布局:Autolayout 简介: 在以前的iOS程序中,是如何设置布局UI界面的? 经常编写大量的坐标计算代码 为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还 ...

  2. Xcode6中自动布局autolayout和sizeclass的使用

    Xcode6中自动布局autolayout和sizeclass的使用   一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...

  3. (转)Xcode6中自动布局autolayout和sizeclass的使用

    Xcode6中自动布局autolayout和sizeclass的使用   一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...

  4. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  5. [IOS]IOS UI指南

    [IOS]IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻 ...

  6. IOS UI 第八篇:基本UI

    实现图片的滚动,并且自动停止在每张图片上     - (void)viewDidLoad{    [super viewDidLoad]; UIScrollView *scrollView = [[U ...

  7. 国外IOS UI指南

    国外IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻译) ...

  8. iOS UI的几种模式

    iOS UI的几种模式: 1.平凡模式(原生控件组合): 2.新闻模式: 3.播放器模式: 4.微博模式:

  9. 通过实现一个TableView来理解iOS UI编程

    推荐一篇神作: 通过实现一个TableView来理解iOS UI编程 http://blog.jobbole.com/61101/

  10. iOS 自动布局 Autolayout 优先级的使用

    一.约束的优先级 0.屏幕适配 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> siz ...

随机推荐

  1. 转!idea启动后发现tomcat前面出现红色或是灰色的问号

    原博文地址:https://blog.csdn.net/z_zhy/article/details/83068168 直接在idea里 点击File------settings,在搜索框直接搜tomc ...

  2. Oil Skimming---hdu4185(最大匹配)

    题目链接 题意:有一个地图.代表水#代表油每个单元格是10*10的,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油: 每次提取的时候勺子放的位置都要是油,不然就被污染而没有价值了: ...

  3. Python开发【模块】:邮件

    邮件 1.简单发送 settings.py配置: import os import sys,string from bin.start import BASE_DIR # 日志存放地址 RUN_LOG ...

  4. kubernetes实战(五):k8s持久化安装Redis Sentinel

    1.PV创建 在nfs或者其他类型后端存储创建pv,首先创建共享目录 [root@nfs ~]# cat /etc/exports /k8s/redis-sentinel/ *(rw,sync,no_ ...

  5. unittest框架(二)单元测试及测试报告

    如果要自测代码,可使用单元测试,需要导入unittest模块,import unittest即可. 例如,自测一个计算连个数相除的函数,代码如下: import unittest def calc(a ...

  6. sublime text 2windows下常用快捷键

    Ctrl + X 删除行 Ctrl + L 选取行 Ctrl + Shift + M 选取括号内的所有东西 Ctrl + Shift + D 复制行 Ctrl + Enter 插入下一行 Ctrl + ...

  7. html结构和标签

    <!DOCTYPE html><meta charset="utf-8"><header>表示页面的一个内容区块,或整个页面的标题</he ...

  8. http之post和get请求的区别

    GET请求 GET /books/?sex=man&name=Professional HTTP/1.1 Host: www.wrox.com User-Agent: Mozilla/5.0 ...

  9. D题:数学题(贪心+二分)

    原题大意:原题链接  题解链接 给定两个集合元素,求出两集合间任意两元素相除后得到的新集合中的第k大值 #include<cstdio> #include<algorithm> ...

  10. MySQL从删库到跑路(三)——SQL语言

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.SQL语言简介 1.SQL语言简介 SQL是结构化查询语言(Structured Query Language) ...