view.bounds.origin是什么意思? [英] What's the meaning of view.bounds.origin?

查看:155
本文介绍了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];

这将导致此结果:

this will cause this result:

那么为什么黄色视图位于那里?

so why the yellow view is located there?

推荐答案

来自文档


在屏幕上,bounds矩形表示相同的可见
视图的一部分作为其框架矩形。默认情况下,边界矩形的
的原点设置为(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.

改变界限所做的工作实际上是翻译 subView 的内部坐标空间向下和向右50个点。然后你在 subView 的坐标空间中添加了一个原点为0的'subsubView' - 因此这是在可见原点的左上角50点 subView

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 's 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.

边界设置为不具有(0,0)原点的东西类似于添加翻译到视图。在几乎所有情况下,这不是您想要做的,而是应该设置 frame 属性。视图层次结构中的每个视图都有自己的坐标空间。 frame 是视图与其superview相关的位置, 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.

因此,子视图的框架在superview的边界中描述了它的位置和大小。如果超级视图的边界具有非零原点,并且您添加了具有零原点的框架的子视图,它将超出超视范围的界限。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆