iOS编程(双语版)-视图-Frame/Bounds/Center

1. Frame

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

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

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

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

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];
v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];
UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(41, 56, 132, 194)];
v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];
UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(43, 197, 160, 230)];
v3.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];

[mainview addSubview: v1];
[v1 addSubview: v2];
[mainview addSubview: v3];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)
let v2 = UIView(frame:CGRectMake(41, 56, 132, 194))
v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)
let v3 = UIView(frame:CGRectMake(43, 197, 160, 230))
v3.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)

mainview.addSubview(v1)
v1.addSubview(v2)
mainview.addSubview(v3)

 

运行结果:

 

2. Bounds

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

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

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];
v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];

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

[mainview addSubview: v1];
[v1 addSubview: v2];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)

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

mainview.addSubview(v1)
v1.addSubview(v2)

 运行结果:

 

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

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];
v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];

UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, 10, 10)];
v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];

[mainview addSubview: v1];
[v1 addSubview: v2];

// 重定义子视图的bounds
CGRect r = v2.bounds;
r.size.height += 20;
r.size.width += 20;
v2.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)

let v2 = UIView(frame:v1.bounds.insetBy(dx: 10, dy: 10))
v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)

mainview.addSubview(v1)
v1.addSubview(v2)

v2.bounds.size.height += 20
v2.bounds.size.width += 20

 运行结果:

 

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

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)];
v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];

UIView* v2 = [[UIView alloc] initWithFrame:CGRectInset(v1.bounds, 10, 10)];
v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];

[mainview addSubview: v1];
[v1 addSubview: v2];

// 改变父视图的原点坐标
CGRect r = v1.bounds;
r.origin.x += 10;
r.origin.y += 10;
v1.bounds = r;

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
v1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1)

let v2 = UIView(frame:v1.bounds.insetBy(dx: 10, dy: 10))
v2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1)

mainview.addSubview(v1)
v1.addSubview(v2)

// 改变父视图的原点坐标
v1.bounds.origin.x += 10
v1.bounds.origin.y += 10

 

运行结果:

 

3. Center

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

 

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

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

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

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

 

5. 关于frame和bounds的区别

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

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

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

 

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

 

时间: 2024-07-31 09:32:17

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

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

1. 什么是视图? 视图显示为手机上的一块矩形区域,管理该区域的所有屏幕显示,它是UIView或者UIView的子类. 视图既可以从xib生成,也可以用代码生成.   2. 窗口 窗口是UIWindow或者它的子类. 视图结构的顶层便是app的窗口. 窗口必须充满设备的屏幕,因此,必须设置窗口的frame为屏幕的bounds. (稍后我会讲解frame和bounds的区别) 代码如下: Objective-C UIWindow* w = [[UIWindow alloc] initWithFra

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

一谈到Autolayout,初学者肯定想到的是IB中使用拖拽啊,pin啊各种鼠标操作来进行添加各种约束. 今天我们要聊得是如何利用代码来添加视图间的约束. 我们来看一个例子: (Objective-C代码) UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(100, 111, 132, 194)]; v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];

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

视图有一个transform属性,它描述了应该如何绘制该视图. 该属性是CGAffineTransform结构体,它代表了3 x 3的变换矩阵(线性代数). 下面的代码让两个矩形视图旋转45度 (Objective-C代码) UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132, 194)]; v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1

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

如何创建一个空的项目,最早的时候XCode的项目想到中,还有Empty Application template这个选项,后来Apple把它 给去掉了. 我们创建一个单视图项目. 1) 删除main.storyboard 2) 删除ViewController相关文件 3) 删除AppDelegate的所有内容   给你的AppDelegate加入如下内容: (Objective-C代码) - (BOOL)application:(UIApplication *)application didF

iOS开发UI基础—手写控件,frame,center和bounds属性

一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View Contollor和view的关系 2.注意点 在OC开发中,Storyboard中的所有操作都可以通过代码实现,程序员一定要熟练掌握代码布局界面的能力! 设置控件监听方法的示例代码如下: [btn addTarget:self action:@selector(click:) forContro

iOS 委托与文本输入(内容根据iOS编程编写)_IOS

•文本框(UITextField) 本章节继续编辑 JXHypnoNerd .文件地址 . 首先我们继续编辑 JXHypnosisViewController.m 修改 loadView 方法,向 view 中添加一个 UITextField 对象: #import "JXHypnosisViewController.h" #import "JXHypnosisView.h" @interface JXHypnosisViewController () @end @

《iOS编程指南》——2.5节准备你的iOS设备

2.5 准备你的iOS设备 iOS编程指南 在设备上部署应用之前,你需要按照图2-5所示的步骤来做相关配置. 如果你已经加入了iOS开发者计划的标准版或者企业版,你现在就可以生成相应的证书和描述文件(Provisioning Profile)了,通过这个证书和描述文件,你才能把下一章中的测试程序安装到你的iOS设备上. 证书和描述文件 Xcode构建工程的时候需要使用开发证书来对二进制文件进行签名,这个开发证书同时也是你作为iOS开发者的唯一标识.Xcode会从Mac OS X的Keychain

《iOS编程指南》——2.1节注册成为iOS开发者

2.1 注册成为iOS开发者 iOS编程指南 在不久之前,获取Xcode需要先注册为苹果开发者.现在不必了,你从Mac App Store直接就可以下载最新本的Xcode和iOS SDK. 如果你还在使用OS X 10.6(Snow Leopard),那么你需要先注册为苹果开发者,成为iOS开发者计划的一员,然后从iOS Dev Center下载Xcode 4和iOS SDK,因为Mac App Store上提供的Xcode的版本只兼容OS X 10.7(Lion)和10.8(Mountain

IOS实现简易版的QQ下拉列表_IOS

下面我们通过实例代码来一步步看怎么实现, 首先建立了两个模型类, 一个Friend, 一个FriendGroup类. 数据源用的本地的一个plist文件. plist文件中包含了FriendGroup的name,friends数组等属性. Friend.h 示例代码 #import <Foundation/Foundation.h> @interface Friend : NSObject @property (nonatomic, copy) NSString *name; @end Fri