//
// 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. 过千万、亿条数据的mysql表更新 mysql 线程状态

    分段更新 UPDATE question SET `status`=1 WHERE status!=1 LIMIT 3000;UPDATE answer SET `status`=1 WHERE st ...

  2. mysql 数据操作 单表查询 使用正则表达式查询

    SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEXP 'on$'; SEL ...

  3. java-mybaits-00201-DAO-SqlSession使用范围

    1.SqlSession的使用范围 SqlSession中封装了对数据库的操作,如:查询.插入.更新.删除等. 通过SqlSessionFactory创建SqlSession,而SqlSessionF ...

  4. Underscore.js (1.7.0)-函数预览

    集合(Collections)(25) - each - map - reduce - reduceRight - find - filter - where - findWhere - reject ...

  5. Spark Shuffle(一)ShuffleWrite:Executor如何将Shuffle的结果进行归并写到数据文件中去(转载)

    转载自:https://blog.csdn.net/raintungli/article/details/70807376 当Executor进行reduce运算的时候,生成运算结果的临时Shuffl ...

  6. 2.3 The Object Model -- Computed Properties

    一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...

  7. SEO笔记:Anatomy of a URL

    Dr. Peter J. Meyers 原文链接:https://moz.com/blog/seo-cheat-sheet-anatomy-of-a-url 原文主要通过对比讲解 SEO优化后的URL ...

  8. (转)如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐)

    如何让ActiveXObject( "Microsoft.XmlDom ")对象在非IE浏览器下显示数据?firefox(火狐) 2013-09-10 16:01 2152人阅读 ...

  9. curl基本使用

    curl简介 linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载. curl可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协 ...

  10. WebService—CXF—实现接口发布和客户端调用

    (一)接口发布的几种方式 定义接口: @WebService(targetNamespace="http://www.itfad.net/queryUser") public in ...