iOS UI-自动布局(AutoLayout)
//
// 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 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:<#(NSString *)#> options:<#(NSLayoutFormatOptions)#> metrics:<#(NSDictionary *)#> views:<#(NSDictionary *)#>]
//
// 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)的更多相关文章
- iOS:自动布局Autolayout
自动布局:Autolayout 简介: 在以前的iOS程序中,是如何设置布局UI界面的? 经常编写大量的坐标计算代码 为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还 ...
- Xcode6中自动布局autolayout和sizeclass的使用
Xcode6中自动布局autolayout和sizeclass的使用 一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...
- (转)Xcode6中自动布局autolayout和sizeclass的使用
Xcode6中自动布局autolayout和sizeclass的使用 一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- [IOS]IOS UI指南
[IOS]IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻 ...
- IOS UI 第八篇:基本UI
实现图片的滚动,并且自动停止在每张图片上 - (void)viewDidLoad{ [super viewDidLoad]; UIScrollView *scrollView = [[U ...
- 国外IOS UI指南
国外IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻译) ...
- iOS UI的几种模式
iOS UI的几种模式: 1.平凡模式(原生控件组合): 2.新闻模式: 3.播放器模式: 4.微博模式:
- 通过实现一个TableView来理解iOS UI编程
推荐一篇神作: 通过实现一个TableView来理解iOS UI编程 http://blog.jobbole.com/61101/
- iOS 自动布局 Autolayout 优先级的使用
一.约束的优先级 0.屏幕适配 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> siz ...
随机推荐
- Tfs 2015 代理池配置笔记
Tfs的构建代理池其实是在代理服务器上开启一个TFSBuild的代理服务,配好相关的Tfs地址后,就能在Tfs管理界面看到了. 如果是Tfs服务和发布代理是同一台服务器,具体操作详见: 安装TFS20 ...
- flask中Flask()和Blueprint() flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法
1.Blueprint()在蓝本注册函数register_blueprint()中,第一个参数为所注册的蓝本名称.当我们在应用对象上注册一个蓝图时,需要指定一个url_prefix关键字 参数(这个参 ...
- Unknown Treasure---hdu5446(卢卡斯+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5446 C(n, m) % (p1*p2*p3*...*pk)的值 其实这个就是中国剩余定理最后算出结果 ...
- python面向对象(类的成员及类方法)
类的普通成员 字段 方法 属性 类的高级成员 静态字段 静态方法 属性方法 类方法 类成员修饰符 类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中, ...
- 洛谷P1373 小a和uim之大逃离 dp
正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...
- Python的Tornado框架的异步任务与AsyncHTTPClient
转载自http://www.php.cn/python-tutorials-284773.html 高性能服务器TornadoPython的web框架名目繁多,各有千秋.正如光荣属于希腊,伟大属于罗马 ...
- Docker+Jenkins_自动化持续集成环境搭建
前一篇文章里已经在内网环境搭建好docker 详见:https://www.cnblogs.com/befer/p/9107503.html, 现在接着搭建一个Docker+Jenkins的集成环境 ...
- Windows中杀死某个端口的进程
最近写项目,总是出现端口被占用的问题,原来傻傻的把电脑重启一下,终于有一天受不了了,想要想办法解决.刚开始从网上找了好多教程,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程( ...
- Replication--复制与CDC和镜像
复制和CDC 都是使用logreader来从日志中读取数据的变更,然后写入到分发库(复制)或变更表中. 1> 单独创建复制发布 在该情况下,会在分发服务器上创建日志读取代理作业(ServerNa ...
- Java-小技巧-003-static、final、static final的区别
final可以修饰:属性,方法,类,局部变量(方法中的变量) final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变,jvm会将其分配到常量池中,程序不可改变其值: final修 ...