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 ...
随机推荐
- android的一些类库的优缺点
经过本人的面试经验,以及接触的android项目,总结了一下android的一些类库的优缺点: 一,线程方面 1.AsyncTask 首先是线程优化以及缺陷方面,针对目前大多数类库来说,都有好的设计方 ...
- 内核通信之Netlink源码分析-用户内核通信原理3
2017-07-06 上节主讲了用户层通过netlink和内核交互的详细过程,本节分析下用户层接收数据的过程…… 有了之前基础知识的介绍,用户层接收数据只涉及到一个核心调用readmsg(), 其他的 ...
- Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中的输入流 第一节课
Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中 ...
- POI - Excel API
一.概述 1. Apache POI是Apache软件基金会的开放源码函式库,POI提供API给java程式对Microsoft Office格式档案读和写的功能. 2. 结构 ...
- PAT 1100 Mars Numbers[难]
1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called &q ...
- linux centos7 erlang rabbitmq安装
最终的安装目录为/opt/erlang 和 /opt/rabbitmq wget http://erlang.org/download/otp_src_21.0.tar.gztar zxvf otp_ ...
- ZW网络团队及资源简介
ZW网络团队及资源简介 ZW网络推广团队,是国内首个教父级网络营销团队,自1997年以来,先后参与操盘多个重大互联网项目,服务过超过150家国际500强客户,是微软公司首家官方认证的网络公关服务商,新 ...
- 20155331 2016-2017-2 《Java程序设计》第8周学习总结
20155331 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了cle ...
- 对Spring Bean了解一二
这之前从未听说过Spring Bean,今天因为学习的<Java核心36讲>其中一篇涉及到了这个内容,因自己基础薄弱,杨晓峰老师讲的内容需要一定的基础才能看懂,故在网上搜罗一些我能理解的关 ...
- 使用idea创建JavaWeb项目
[第一步] File---New---Project [第二步] 选择Java Enterprise版本,然后配置tomcat 注意:这里关联的tomcat home指的是tomcat的解压目录(bi ...