UIView frame, bounds and center
http://stackoverflow.com/questions/5361369/uiview-frame-bounds-and-center
Since the question I asked has been seen many times I will provide a detailed answer of it. Feel free to modify it if you want to add more correct content.
First a recap on the question: frame, bounds and center and theirs relationships.
Frame A view's frame
(CGRect
) is the position of its rectangle in the superview
's coordinate system. By default it starts at the top left.
Bounds A view's bounds
(CGRect
) expresses a view rectangle in its own coordinate system.
Center A center
is a CGPoint
expressed in terms of the superview
's coordinate system and it determines the position of the exact center point of the view.
Taken from UIView + position these are the relationships (they don't work in code since they are informal equations) among the previous properties:
frame.origin = center - (bounds.size / 2.0)
center = frame.origin + (bounds.size / 2.0)
frame.size = bounds.size
NOTE: These relationships do not apply if views are rotated. For further info, I will suggest you take a look at the following image taken from The Kitchen Drawer based on Stanford CS193p course. Credits goes to @Rhubarb.
Using the frame
allows you to reposition and/or resize a view within its superview
. Usually can be used from a superview
, for example, when you create a specific subview. For example:
// view1 will be positioned at x = 30, y = 20 starting the top left corner of [self view]
// [self view] could be the view managed by a UIViewController
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(30.0f, 20.0f, 400.0f, 400.0f)];
view1.backgroundColor = [UIColor redColor];
[[self view] addSubview:view1];
When you need the coordinates to drawing inside a view
you usually refer to bounds
. A typical example could be to draw within a view
a subview as an inset of the first. Drawing the subview requires to know the bounds
of the superview. For example:
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 400.0f, 400.0f)];
view1.backgroundColor = [UIColor redColor];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectInset(view1.bounds, 20.0f, 20.0f)];
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];
Different behaviours happen when you change the bounds
of a view. For example, if you change the bounds
size
, the frame
changes (and vice versa). The change happens around the center
of the view. Use the code below and see what happens:
NSLog(@"Old Frame %@", NSStringFromCGRect(view2.frame));
NSLog(@"Old Center %@", NSStringFromCGPoint(view2.center));
CGRect frame = view2.bounds;
frame.size.height += 20.0f;
frame.size.width += 20.0f;
view2.bounds = frame;
NSLog(@"New Frame %@", NSStringFromCGRect(view2.frame));
NSLog(@"New Center %@", NSStringFromCGPoint(view2.center));
Furthermore, if you change bounds
origin
you change the origin
of its internal coordinate system. By default the origin
is at (0.0, 0.0)
(top left corner). For example, if you change the origin
for view1
you can see (comment the previous code if you want) that now the top left corner for view2
touches the view1
one. The motivation is quite simple. You say to view1
that its top left corner now is at the position (20.0, 20.0)
but since view2
's frame
origin
starts from (20.0, 20.0)
, they will coincide.
CGRect frame = view1.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view1.bounds = frame;
The origin
represents the view
's position within its superview
but describes the position of the bounds
center.
Finally, bounds
and origin
are not related concepts. Both allow to derive the frame
of a view (See previous equations).
View1's case study
Here is what happens when using the following snippet.
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(30.0f, 20.0f, 400.0f, 400.0f)];
view1.backgroundColor = [UIColor redColor];
[[self view] addSubview:view1];
NSLog(@"view1's frame is: %@", NSStringFromCGRect([view1 frame]));
NSLog(@"view1's bounds is: %@", NSStringFromCGRect([view1 bounds]));
NSLog(@"view1's center is: %@", NSStringFromCGPoint([view1 center]));
The relative image.
This instead what happens if I change [self view]
bounds like the following.
// previous code here...
CGRect rect = [[self view] bounds];
rect.origin.x += 30.0f;
rect.origin.y += 20.0f;
[[self view] setBounds:rect];
The relative image.
Here you say to [self view]
that its top left corner now is at the position (30.0, 20.0) but since view1
's frame origin starts from (30.0, 20.0), they will coincide.
Additional references (to update with other references if you want)
About clipsToBounds
(source Apple doc)
Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.
In other words, if a view's frame
is (0, 0, 100, 100)
and its subview is (90, 90, 30, 30)
, you will see only a part of that subview. The latter won't exceed the bounds of the parent view.
masksToBounds
is equivalent to clipsToBounds
. Instead to a UIView
, this property is applied to a CALayer
. Under the hood, clipsToBounds
calls masksToBounds
. For further references take a look to How is the relation between UIView's clipsToBounds and CALayer's masksToBounds?.
UIView frame, bounds and center的更多相关文章
- frame.bounds和center
CGPoint point=CGPoint(x,y); //表示位置 CGSize size=CGSzieMake(width,height); //表示大小 CGRect rect=CGRect ...
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...
- ios开发之UIView的frame、bounds跟center属性的区别(附图)
博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...
- UIView 中 frame, bounds, center 属性的关系
最近一直在学 iOS 开发,所以专门创建了这样一个类别,将自己学习中的一些问题整理,记录下来.由于自己是初学者,所以所写的文章非常基础,写这个类别一是为了给自己留下存 档,二是为了给和我有同样问题的初 ...
- 详解UIView的frame、bounds和center属性
From: http://ios.wpjam.com/2011/08/29/uiview-frame-bounds-center/ 1.概要 翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下 ...
- iOS 中的frame,bounds,center,transform关联
这里有一篇好文章 http://www.winddisk.com/2012/06/07/transform/ 先看几个知识点,UIView 的frame,bounds,center,transform ...
- 初见IOS的UI之:UI控件的属性frame bounds center 和transform
这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...
- UIView 中bounds和frame的差别
搞iOS开发的童鞋基本都会用过UIView,那他的bounds和frame两个属性也不会陌生,那这两个有什么实质性的区别呢? 先看到下面的代码你肯定就明白了一些: -(CGRect)frame{ ...
- iOS编程(双语版)-视图-Frame/Bounds/Center
1. Frame 每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置. (屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展) 设置frame通常通过 ...
随机推荐
- mod_wsgi
配置: WSGIScriptAlias /var/www/wsgi-scripts/simple.wsgi def application(environ, start_response): outp ...
- yaffs2文件系统
1 .yaffs2源码目录文件复制到需要移植的linux内核目录fs/下 同时替换掉源码文件中的Makefile文件跟Kconfig文件. 2.在内核中添加对yaffs2的支持. 3.在make me ...
- linux驱动(一)
编写模块必须先声明下面两句: #include <linux/module.h> //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关 #i ...
- js禁止高频率连续点击思路
1.类似react的数据流,点击之后立即设置值为空,当返回值后才可以点击 2.设置定时器,每次进入之前先清空掉定时器,然后开启定时器 <main> <div id="me& ...
- javascript 学习笔记之模块化编程
题外: 进行web开发3年多了,javascript(后称js)用的也比较多,但是大部分都局限于函数的层次,有些公共的js函数可重用性不好,造成了程序的大量冗余,可读性差(虽然一直保留着注释的习惯,但 ...
- Quartz 之 windowService
(一)创建服务 QuarzService using System.ServiceProcess;using System.Text; using Quartz;using Quartz.Impl; ...
- AS3.0的动态类和密封类
动态类:生成的实例可以在运行时动态添加属性和方法.类名前有dynamic就是动态类 密封类:生成的实例不可以在运行时动态添加属性和方法
- PHP通过(PDO)Mysql表字段一键生成创建sqlite的SQL
首发于:http://www.zzzzy.com/201406053158.html /** * Mysql表字段一键生成创建sqlite的SQL 2 * @author: Skiychan < ...
- SimpleMembership: The future of membership for ASP.NET
http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal- ...
- OC 之 const
1. 修饰变量 一般设置传参数的时候 若设置为const, 则在调用过程中不允许修改参数值;(readonly) // *前const: 不能通过指针, 改变p指向的值 const int *p = ...