gpt4 book ai didi

iphone - view.bounds.origin 是什么意思?

转载 作者:可可西里 更新时间:2023-11-01 03:08:40 25 4
gpt4 key购买 nike

如果我将 view.bounds.origin 设置为 (50,50) ,那么 subview 将绘制 (50,50) 左 View 。但是我觉得应该是相反的结果,那么bounds.origin是什么意思呢?

对不起大家,我不是母语人士,所以这次我放了这个示例代码和图片~~

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];

这将导致这样的结果: enter image description here

那么为什么黄色 View 位于那里?

最佳答案

来自documentation :

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 .

如果您设置了 subViewframe 而不是 bounds,您将在其中移动 subView superview 的坐标空间,因此您的蓝色方 block 会向上和向左移动,而黄色方 block 将包含在其中并具有相同的原点。

bounds 设置为原点不是 (0,0) 的内容类似于向 View 添加翻译。在几乎所有情况下,这都不是您想要做的,您应该改为设置 frame 属性。 View 层次结构中的每个 View 都有自己的坐标空间。 frame 是 View 与其父 View 相关的位置,bounds 是 View 内的空间。

因此, subview 的 frame 描述了它在父 View 的 bounds 中的位置和大小。如果父 View 的 bounds 具有非零原点,并且您添加了一个 subview ,其 frame 具有零原点,它将位于父 View 边界的“外部”。

关于iphone - view.bounds.origin 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7872552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com