1. Frame

每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置

(屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展)

设置frame通常通过视图的指定初始化器initWithFrame

下面来看个例子,该例子初始化了3个相互叠加的矩形区域

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:];
UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:];
UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v3.backgroundColor = [UIColor colorWithRed: green: blue: alpha:]; [mainview addSubview: v1];
[v1 addSubview: v2];
[mainview addSubview: v3];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(, , , ))
v1.backgroundColor = UIColor(red: , green: 0.4, blue: , alpha: )
let v2 = UIView(frame:CGRectMake(, , , ))
v2.backgroundColor = UIColor(red: 0.5, green: , blue: , alpha: )
let v3 = UIView(frame:CGRectMake(, , , ))
v3.backgroundColor = UIColor(red: , green: , blue: , alpha: ) mainview.addSubview(v1)
v1.addSubview(v2)
mainview.addSubview(v3)

运行结果:

2. Bounds

Bounds也是CGRect结构,和Frame不同,它描述的是视图自身的矩形区域,是相对于自身的坐标系而言的。

下面的例子创建了2个叠加的矩形视图,子视图为绿色较小的那个

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:]; // 在一个视图内部画图时,通常需要使用该视图的bounds
UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, , )];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:]; [mainview addSubview: v1];
[v1 addSubview: v2];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(, , , ))
v1.backgroundColor = UIColor(red: , green: 0.4, blue: , alpha: ) // 在一个视图内部画图时,通常需要使用该视图的bounds
let v2 = UIView(frame:v1.bounds.insetBy(dx: , dy: ))
v2.backgroundColor = UIColor(red: 0.5, green: , blue: , alpha: ) mainview.addSubview(v1)
v1.addSubview(v2)

运行结果:

下面的例子通过改变绿色子视图的bounds将父视图完全覆盖

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:]; UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, , )];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:]; [mainview addSubview: v1];
[v1 addSubview: v2]; // 重定义子视图的bounds
CGRect r = v2.bounds;
r.size.height += ;
r.size.width += ;
v2.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(, , , ))
v1.backgroundColor = UIColor(red: , green: 0.4, blue: , alpha: ) let v2 = UIView(frame:v1.bounds.insetBy(dx: , dy: ))
v2.backgroundColor = UIColor(red: 0.5, green: , blue: , alpha: ) mainview.addSubview(v1)
v1.addSubview(v2) v2.bounds.size.height +=
v2.bounds.size.width +=

运行结果:

下面的例子,紫色父视图的原点进行少量偏移

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:]; UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, , )];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:]; [mainview addSubview: v1];
[v1 addSubview: v2]; // 改变父视图的原点坐标
CGRect r = v1.bounds;
r.origin.x += ;
r.origin.y += ;
v1.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(, , , ))
v1.backgroundColor = UIColor(red: , green: 0.4, blue: , alpha: ) let v2 = UIView(frame:v1.bounds.insetBy(dx: , dy: ))
v2.backgroundColor = UIColor(red: 0.5, green: , blue: , alpha: ) mainview.addSubview(v1)
v1.addSubview(v2) // 改变父视图的原点坐标
v1.bounds.origin.x +=
v1.bounds.origin.y +=

运行结果:

3. Center

Center即视图的中心点位置坐标

4. 关于主窗口和设备屏幕

设备屏幕(UIScreen.mainScreen())没有frame, 但它有bounds。

主窗口没有父视图,但是它的frame可以设为屏幕的bounds。

let w = UIWindow(frame: UIScreen.mainScreen().bounds)

5. 关于frame和bounds的区别

一言以蔽之,就是前者相对于父视图,而后者相对于自身。

我们还是用图片来看一下吧,这样更直观

在视图未旋转的情况下,它们差不多,坐标稍有区别,如下图:

而在视图作了类似旋转的transform之后,它们的坐标则有很大的差别了,见下图:

iOS编程(双语版)-视图-Frame/Bounds/Center的更多相关文章

  1. iOS编程(双语版) - 视图 - 基本概念

    1. 什么是视图? 视图显示为手机上的一块矩形区域,管理该区域的所有屏幕显示,它是UIView或者UIView的子类. 视图既可以从xib生成,也可以用代码生成. 2. 窗口 窗口是UIWindow或 ...

  2. iOS编程(双语版)-视图-Autolayout代码初步

    一谈到Autolayout,初学者肯定想到的是IB中使用拖拽啊,pin啊各种鼠标操作来进行添加各种约束. 今天我们要聊得是如何利用代码来添加视图间的约束. 我们来看一个例子: (Objective-C ...

  3. iOS编程(双语版) - 视图 - Transform(转换)

    视图有一个transform属性,它描述了应该如何绘制该视图. 该属性是CGAffineTransform结构体,它代表了3 x 3的变换矩阵(线性代数). 下面的代码让两个矩形视图旋转45度 (Ob ...

  4. iOS编程(双语版) - 视图 - 手工代码(不使用向导)创建视图

    如何创建一个空的项目,最早的时候XCode的项目想到中,还有Empty Application template这个选项,后来Apple把它 给去掉了. 我们创建一个单视图项目. 1) 删除main. ...

  5. 初见IOS的UI之:UI控件的属性frame bounds center 和transform

    这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...

  6. UIView 中 frame, bounds, center 属性的关系

    最近一直在学 iOS 开发,所以专门创建了这样一个类别,将自己学习中的一些问题整理,记录下来.由于自己是初学者,所以所写的文章非常基础,写这个类别一是为了给自己留下存 档,二是为了给和我有同样问题的初 ...

  7. frame,bounds,center分析

    采用CGPoint来表示坐标系X,Y位置,创建一个坐标的方式为:CGPoint point=CGPointMake(x,y) CGSize用来表示视图的宽度和高度,可以用CGSizeMake(widt ...

  8. frame,bounds,center-三者的含义

    frame与bounds的区别比较 frame,bounds,center-三者的含义 偶然觉的,这三个属性有时候定位的时候,需要用.于是就来搞清楚,到底frame,bounds,center 这三个 ...

  9. iOS开发UI篇—手写控件,frame,center和bounds属性

    iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

随机推荐

  1. The .NET weak event pattern in C#

    Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...

  2. .NET中常见的内存泄漏和解决办法

    在.NET中,虽然CLR的GC垃圾回收器帮我们自动回收托管堆对象,释放内存,最大程度避免了"内存泄漏"(应用程序所占用的内存没有得到及时释放),但.NET应用程序"内存泄 ...

  3. Delphi来实现一个IP地址输入控件

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. Delphi XE5 Android 运行黑屏卡死的解决方法

    1. 确保正确安装Android SDK: 开始菜单 > 所有程序 > Embarcadero RAD Studio XE5 > > Android Tools > 打开 ...

  5. Python index()方法

    Python index()方法  Python 字符串 描述 Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否 ...

  6. Java 获取客户端IP

    像移动网关一样,iisforward这个ISAPI过滤器也会对request对象进行再包装,附加一些WLS要用的头信息.这种情况下,直接用request.getRemoteAddr()是无法取到真正的 ...

  7. eclipse 中 import sun.misc.BASE64Decoder; 报错

    from://http://blog.sina.com.cn/s/blog_48964b120101ahrf.html 在android做3DES加密功能时 eclipse 中 import sun. ...

  8. 用drawRect以及CAReplicatorLayer绘制动态水波纹

    用drawRect以及CAReplicatorLayer绘制动态水波纹 大大简化了写水波纹效果的难度,你可以根据示例自己组装水波纹效果,本设计是几个工具组合在一起完成的效果, DrawRectObje ...

  9. 裂痕第一至五季/以法之名Damages迅雷下载

    本季第一至五季Damages Season (2007-2012)看点:<裂痕>又是一部以法律剧情为主打,其间又掺杂着悬念,阴谋,破案等因素的剧集.女主角帕蒂-赫韦斯(Patty Hewe ...

  10. TrafficStats——流量统计类的范例,获取实时网速

    2.3开始android就提供来这个类的API,这样我们就可以方便的用他来实现统计手机流量来.这个类其实也很简单,我贴上他的几个方法,大家一看就知道怎么用了. static long getMobil ...