目录:

1. 纯代码布局

2. 在View中进行代码布局

3. 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)的更多相关文章

  1. iOS系列译文:自定义Collection View布局

    原文出处: Ole Begemann   译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...

  2. IOS UIView 03- 自定义 Collection View 布局

    注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...

  3. 自定义 Collection View 布局

    自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...

  4. 自定义Collection View布局

    转自answer-huang的博客 原文出自:Custom Collection View Layouts    UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...

  5. Android View框架总结(五)View布局流程之Layout

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52216195 View树的Layout流程 View的Layout时序图 View布局 ...

  6. Android View框架总结(四)View布局流程之Measure

    View树的measure流程 View的measures时序图 View布局流程之measure measure过程 View的measure过程 ViewGroup的measure过程 Frame ...

  7. Android GUI之View布局

    在清楚了View绘制机制中的第一步测量之后,我们继续来了解分析View绘制的第二个过程,那就是布局定位.继续跟踪分析源码,根据之前的流程分析我们知道View的绘制是从RootViewImpl的perf ...

  8. 转载:微信小程序view布局

    https://www.cnblogs.com/sun8134/p/6395947.html

  9. Android 在程序中动态添加 View 布局或控件

    有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法: 1.addView 添加View到布局容器 2.removeView 在布局容器中删掉已有的View 3.LayoutPar ...

随机推荐

  1. C#多线程编程简述

    NET将关于多线程的功能定义在System.Threading名字空间中.因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;). a.启动线程顾名思义,“启动 ...

  2. js字符串大小写转换

    var str = 'hello world'; alert(str.toUpperCase());//将字符串转换为大写. alert(str.toLowerCase());//将字符串转换为小写. ...

  3. Stack集合、queue集合、hashtable集合

    1.栈:Stack,先进后出,一个一个赋值,一个一个取值,按顺序. .count           取集合内元素的个数 .push()         将元素一个一个推入集合中//stack集合存入 ...

  4. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )

    维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...

  5. tomcat手动发布

    有些时候不能通过eclipse中的server服务发布工程,这时候就只能通过手动配置进行发布了 如下: 配置发布路径:D:\Program Files\apache-tomcat-6.0.10\con ...

  6. Nginx阅读笔记(二)之location的用法

    两个配置文件 一: server { listen 80; # # 在本机所有ip上监听80,也可以写为192.168.1.202:80,这样的话,就只监听192.168.1.202上的80口 ser ...

  7. qt qml 利用xmlhttprequest 调用有赞api

    最近朋友在有赞商城上面开了一个店铺,因为有实体店,一般卖商品后送货上门,但是打票时候老是人工用world文档人工复制黏贴订单打印小票, 所以就找我帮忙做一个软件专门打印小票的,就研究起来调用有赞第三方 ...

  8. utf8格式源代码中的字符串,默认都会当作char来处理,除非用L""符号来修饰

    原先QString("mystrr"),现在都不认了,必须都要加上L才行 原先:m_conn->put_HttpProxyAuthMethod("Basic&quo ...

  9. spring jdbc 笔记3

    spring JDBC 加入对commons-dbcp  spring-jdbc  spring-tx的依赖 1.数据源的配置 获取数据源在spring中的Bean管理默认已经是单例模式 关闭数据源d ...

  10. BZOJ 1601 [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...