view.bounds.origin 是什么意思? [英] What's the meaning of view.bounds.origin?
问题描述
说如果我将 view.bounds.origin 设置为 (50,50) ,那么子视图将被绘制 (50,50) 以供查看.但是我觉得应该是逆结果,那么bounds.origin是什么意思?
say if I set view.bounds.origin to (50,50) ,then the subview is drawn (50,50) left up to view. But I thought it should be the inverse result, so what does bounds.origin mean?
对不起各位,我的母语不是英语,所以这次我放了这个示例代码和图片~~
sorry guys, I'm not a native English speaker,so I put this sample code and image this time~~
subview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
subview.backgroundColor = [UIColor blueColor];
subview.bounds = CGRectMake(50, 50, 200, 200);
subsubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,100,100)];
subsubview.backgroundColor = [UIColor yellowColor];
[subview addSubView:subsubView];
这会导致这样的结果:
为什么黄色视图位于那里?
so why the yellow view is located there?
推荐答案
来自 文档:
在屏幕上,边界矩形代表相同的可见视图的一部分作为其框架矩形.默认情况下,原点边界矩形设置为 (0, 0) 但您可以将此值更改为显示视图的不同部分.
On the screen, the bounds rectangle represents the same visible portion of the view as its frame rectangle. By default, the origin of the bounds rectangle is set to (0, 0) but you can change this value to display different portions of the view.
您通过改变bounds
所做的就是有效地将subView
的内部坐标空间向下和向右平移50 个点.然后,您在 subView
的坐标空间内添加了原点为 0,0 的subsubView"——因此,这是 subView
的可见原点的左上方 50 个点代码>.
What you've done by altering bounds
is effectively to translate subView
's internal coordinate space down and to the right by 50 points. You've then added 'subsubView' with an origin of 0,0 within subView
's coordinate space - this is therefore 50 points up and to the left of the visible origin of subView
.
如果你设置了 subView
的 frame
而不是 bounds
,你就会移动 subView
superview 的 坐标空间,所以你的蓝色方块会向上和向左移动,黄色方块将包含在其中并具有相同的原点.
If you had set subView
's frame
instead of bounds
, you would have moved subView
within it's superview's coordinate space, so your blue square would have moved up and to the left, and the yellow square would be contained within it and have the same origin.
将 bounds
设置为不具有 (0,0) 原点的内容类似于向视图添加翻译.在几乎所有情况下,这都不是您想要做的,您应该改为设置 frame
属性.视图层次结构中的每个视图都有自己的坐标空间.frame
是视图与其父视图相关的位置,bounds
是视图内的空间.
Setting bounds
to something that doesn't have an origin of (0,0) is similar to adding a translation to the view. In nearly all circumstances, this isn't what you want to do, and you should be setting the frame
property instead. Each view in a view hierarchy has its own coordinate space. frame
is where a view is in relation to its superview, and bounds
is the space within a view.
因此,子视图的frame
描述了它在父视图的bounds
中的位置和大小.如果超视图的 bounds
具有非零原点,并且您添加具有零原点的 frame
的子视图,它将超出"超视图的边界.
So, a subview's frame
describes its location and size in the superview's bounds
. If the superview's bounds
has a non-zero origin, and you add a subview with a frame
having a zero origin, it is going to be "outside" the superview's bounds.
这篇关于view.bounds.origin 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!