15-UIKit(view布局、Autoresizing)
目录:
1. 纯代码布局
纯代码布局分VC下和V下
[MX1-layout-code]
在VC下覆盖viewDidLayoutSubviews方法,在方法中写布局,一般会把要布局的控件拉成属性
当界面大小发生改变时(竖屏变横屏, 出现工具栏或各种bar....), 调用此方法。注意这个方法在运行之后就会调用,只是在界面发生变化后会再次调用,最终的布局在这里起作用。
此方法的调用在Storyboard布局后。
注意关掉AutoLayout
1.1 计算坐标
1> 两个按钮等宽
2> ImageView:
上下左右永远保持70, 50, 20, 20
3>三个小按钮(20x20), 永远保持在右下角:
// 当界面大小发生改变时(竖屏变横屏, 出现工具栏或各种bar....), 调用此方法。注意这个方法在运行之后就会调用,只是在界面发生变化后会再次调用。
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
// NSLog(@"11111");
CGRect frame = self.button1.frame;
CGFloat buttonWidth = (self.view.bounds.size.width - * - ) / ;
frame.size.width = buttonWidth;
self.button1.frame = frame;
frame.origin.x += buttonWidth + ;
self.button2.frame = frame;
frame = CGRectZero;
frame.origin = CGPointMake(, );
frame.size = CGSizeMake(self.view.bounds.size.width - - , self.view.bounds.size.height - - );
self.imageView.frame = frame;
frame.size = CGSizeMake(, );
frame.origin.x = self.view.bounds.size.width - - - - * ;
frame.origin.y = self.view.bounds.size.height - - ;
self.btn1.frame = frame;
frame.origin.x = self.view.bounds.size.width - - - * ;
self.btn2.frame = frame;
frame.origin.x = self.view.bounds.size.width - - ;
self.btn3.frame = frame;
NSLog(@"调用了viewDidLayoutSubviews方法");
}
1.2 各种Bar的影响
[MX2-Bar-Code]
从iOS7开始,任何一个VC类都拥有两个属性:
.topLayoutGuide
.bottomLayoutGuide
这两属性保存了VC上面和下面各种Bar所占用的空间。
这两属性都是遵守了UILayoutSupport协议的对象,此协议规定了一个属性:length, 用来表示Bar所占的空间。
一般的,这两属性用于VC渗透到上下各种Bar下面的情况下,计算Bar所占用的空间
可以在Xcode中设置VC不渗透到Bar下面去(在第四个检查器勾掉under bottom bars)。此时.topLayoutGuide和.bottomLayoutGuider的length都为0.
所以,不管VC是否渗透到Bar的下面,坐标上都加入这两属性是没错的。
补充:
可以设置VC属性:
.edgesForExtendedLayout = ….
来设置VC是否渗透到Bar下面
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
// 上下都不渗透延伸
self.edgesForExtendedLayout = UIRectEdgeNone;
CGRect frame = self.button.frame;
frame.origin.x = self.view.bounds.size.width - - frame.size.width;
frame.origin.y = ;// 因为在该视图的第三个检查器设置关掉了使用top bars
self.button.frame = frame;
}
1.3 自己绘制的图形
当界面大小发生变化,自动绘制的图形也会有布局问题。
[MX3-Redraw]
view视图有一个属性:contentMode,这个属性决定着当view的大小改变时,做出什么反应。
默认是Scale To Fill 左右上下全拉伸
如果ImageView 一般会选择Aspect Fit /Aspect Fill
如果是自定义的绘制图形,应该设置成Redraw, 这样,当view大小发生改变时,会重绘图形。
// 设置该view的mode为Redraw,那么当界面发生变化时会重新调用这个进行重绘
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path closePath];
[[UIColor redColor] setStroke];
[path stroke];
CGContextRestoreGState(context);
}
2. 在View中进行代码布局
2.1 View类中有一个方法:layoutSubviews, 给view中所有子view布局。
当视图的大小改变时,以下三个方法会被自动依次调用
1> VC: viewWillLayoutSubviews
2> V: layoutSubviews
3> VC: viewDidLayoutSubviews
View:
drawRect: 绘制 setNeedsDisplay
layoutSubviews: 布局 setNeedsLayout
[MX4-ViewLayout] 自定义一个视图(Cell) 用代码进行布局
// 当界面大小改变时或setNeedsLayout调用此方法,这是手动布局,注意关掉自动布局
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat x = self.downLoadedIcon.frame.origin.x;
if (self.music.downloaded) {
x += ;
}
CGRect frame;
if (self.music.highQuality) {
frame = self.highQualityIcon.frame;
frame.origin.x = x;
self.highQualityIcon.frame = frame;
x += ;
}
frame = self.subLabel.frame;
frame.origin.x = x;
self.subLabel.frame = frame;
}
3. Autoresizing
3.1 什么是Autoresizing
是老的布局技术,以前叫spring/struct布局
操作简单, 功能有限,现在依然有效
还可以和AutoLayout技术一起使用。
3.2 工作原理
根据父视图的变化,按父视图变化的比例改变子视图的大小,或者其他计算式来确定子视图的frame.
3.3 怎么用
[MX5-Autoresizing]
外边四条红线约束子视图离边界的间距
里边的两条线用来指示子视图是否可以随父视图的变化而伸缩
[MX6-Autoresizing]
3.4 和代码结合使用
如果Autoresizing技术无法满足需求时,可以用代码补充
3.5 用纯代码方式使用Autoresizing
每个视图都有一个属性:autoresizingMask, 用来描述Autoresizing的设置
- (void)viewDidLoad
{
[super viewDidLoad]; self.button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
}
附:
OC语言中的枚举定义常见用法:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0, //0
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, //1
UIViewAutoresizingFlexibleWidth = 1 << 1, //2
UIViewAutoresizingFlexibleRightMargin = 1 << 2, //4
UIViewAutoresizingFlexibleTopMargin = 1 << 3, //8
UIViewAutoresizingFlexibleHeight = 1 << 4, //16
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 //32
};
1 << 1 :
0000 0001 << 1 : 0000 0010 (2)
1 << 2 :
0000 0001 << 2 : 0000 0100 (4)
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight
0000 0010
0000 0100
0001 0000
-------------
0001 0110 6
作业:
1. 参考H01资源包, 实现播放界面,用代码布局
2. 图片涂鸦
参考PhotoDraw项目
15-UIKit(view布局、Autoresizing)的更多相关文章
- iOS系列译文:自定义Collection View布局
原文出处: Ole Begemann 译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...
- IOS UIView 03- 自定义 Collection View 布局
注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...
- 自定义 Collection View 布局
自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...
- 自定义Collection View布局
转自answer-huang的博客 原文出自:Custom Collection View Layouts UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...
- Android View框架总结(五)View布局流程之Layout
转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52216195 View树的Layout流程 View的Layout时序图 View布局 ...
- Android View框架总结(四)View布局流程之Measure
View树的measure流程 View的measures时序图 View布局流程之measure measure过程 View的measure过程 ViewGroup的measure过程 Frame ...
- Android GUI之View布局
在清楚了View绘制机制中的第一步测量之后,我们继续来了解分析View绘制的第二个过程,那就是布局定位.继续跟踪分析源码,根据之前的流程分析我们知道View的绘制是从RootViewImpl的perf ...
- 转载:微信小程序view布局
https://www.cnblogs.com/sun8134/p/6395947.html
- Android 在程序中动态添加 View 布局或控件
有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法: 1.addView 添加View到布局容器 2.removeView 在布局容器中删掉已有的View 3.LayoutPar ...
随机推荐
- C# Webservice
c#webservice的简单示例 webservice传递的数据只能是序列化的数据,典型的就是xml数据. 下面以一个简单例子为例: (一)新建——-项目---Visual C#---web-- ...
- 调用AnimateWindow API来实现弹出效果
下面是实例的cs代码 public partial class frm_Main : Form { //使用Windows Api AnimateWindow [DllImport("use ...
- 用python快速搭建WEB服务器
cmd下进入你要搞WEB项目的目录 输入↓方代码 python -m SimpleHTTPServer 端口号# 默认是8000 这样就启动了一个简单的WEB服务器
- 在CheckBox中,仅仅允许选择一项
作用相当于RadioButonList <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=" ...
- iOS内存管理 ARC与MRC
想驾驭一门语言,首先要掌握它的内存管理特性.iOS开发经历了MRC到ARC的过程,下面就记录一下本人对iOS内存管理方面的一些理解. 说到iOS开发,肯定离不开objective-c语言(以下简称OC ...
- BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )
维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...
- seajs + easyui [转]
* *content seajs+easyui使用 */ /** * 首先来看看在seajs中jquery和jquery插件如何使用 */ 1.jquery.js define(function(re ...
- 高级UNIX环境编程10 信号
信号是软件中断,提供一种处理异步事件的方法 <signal.h> sigaction()
- (3)选择元素——(2)文档对象模型(The Document Object Model)
One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...
- hadoop源代码解读namenode高可靠:HA;web方式查看namenode下信息;dfs/data决定datanode存储位置
点击browserFilesystem,和命令查看结果一样 当我们查看hadoop源代码时,我们看到hdfs下的hdfs-default.xml文件信息 我们查找${hadoop.tmp.dir}这是 ...