我想知道如何正确使用这些属性.
据我所知,frame
可以从我正在创建的视图的容器中使用.
也可以从我正在创建的视图的容器中使用center
.此属性更改视图相对于其容器的位置.
最后,bounds
是相对于视图本身的.它会更改视图的可绘制区域.
Can you give more info about the relationship between 100 and 101? What about the 102 and 103 properties?
我想知道如何正确使用这些属性.
据我所知,frame
可以从我正在创建的视图的容器中使用.
也可以从我正在创建的视图的容器中使用center
.此属性更改视图相对于其容器的位置.
最后,bounds
是相对于视图本身的.它会更改视图的可绘制区域.
Can you give more info about the relationship between 100 and 101? What about the 102 and 103 properties?
由于我问的问题已经被多次看到,我将提供一个详细的答案.如果你想添加更多正确的内容,请随意修改.
首先回顾一下这个问题:框架、边界、中心以及它们之间的关系.
Frame视图的frame
(CGRect
)是其矩形在superview
坐标系中的位置.默认情况下,它从左上角开始.
Bounds视图bounds
(CGRect
)在其自己的坐标系中表示视图矩形.
Center A center
是用superview
的坐标系表示的CGPoint
,它确定了视图的精确中心点的位置.
取自100,这些是前面的属性之间的关系(它们不能在代码中工作,因为它们是非正式的等式):
frame.origin = center - (bounds.size / 2.0)
center = frame.origin + (bounds.size / 2.0)
frame.size = bounds.size
个
NOTE:如果旋转视图,则这些关系不适用.为了了解更多的信息,我建议你看看下面的图片,这些图片取自100基于101课程拍摄的图片.学分是@Rhubarb分.
使用frame
可以在其superview
中重新定位和/或调整视图大小.通常可以在superview
中使用,例如,在创建特定子视图时.例如:
// 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];
当你需要在view
内作图的坐标时,你通常会参考bounds
.一个典型的例子可以是在view
个子视图内绘制作为第一个子视图的插图.绘制子视图需要知道SuperView的bounds
.例如:
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];
当您更改视图的bounds
时,会出现不同的行为.
例如,如果您更改bounds
size
,则frame
也会更改(反之亦然).更改发生在视图的第center
位左右.使用下面的代码,看看会发生什么:
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));
此外,如果更改bounds
origin
,则会更改其内部坐标系的origin
.默认情况下,origin
位于(0.0, 0.0)
(左上角).例如,如果将origin
更改为view1
,您可以看到(如果愿意,可以注释前面的代码),现在view2
的左上角接触到了view1
.动机很简单.你对view1
说它的左上角现在是(20.0, 20.0)
,但是因为view2
的frame
origin
从(20.0, 20.0)
开始,所以它们会重合.
CGRect frame = view1.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view1.bounds = frame;
origin
代表view
在其superview
内的位置,但描述bounds
中心的位置.
最后,bounds
和origin
是不相关的概念.两者都允许导出视图的frame
(参见前面的公式).
View1's case study
下面是使用以下代码段时发生的情况.
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]));
相对图像.
如果我像下面这样改变[self view]
个边界,会发生什么呢?
// previous code here...
CGRect rect = [[self view] bounds];
rect.origin.x += 30.0f;
rect.origin.y += 20.0f;
[[self view] setBounds:rect];
相对图像.
这里您对[self view]
说它的左上角现在位于位置(30.0,20.0),但是因为view1
的帧原点从(30.0,20.0)开始,所以它们将重合.
Additional references(如果需要,可以使用其他引用进行更新)
大约clipsToBounds
个(苹果文档来源)
将此值设置为"是"将导致子视图被剪裁到边界
换句话说,如果视图的frame
是(0, 0, 100, 100)
,而它的子视图是(90, 90, 30, 30)
,那么您将只能看到该子视图的一部分.后者不会超出父视图的界限.
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?.