ios - 更改 View 边界会影响框架

标签 ios uiview frame cgrect bounds

我正在尝试了解 View 如何响应其边界的更改。如果我更改 View 的边界原点,它会相应地更改框架原点吗?

例如

UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 150, 200)];
greenView.backgroundColor = [UIColor colorWithRed:0.494 green:0.827
                                             blue:0.129 alpha:1];
[self.view addSubview:greenView];
greenView.bounds = CGRectMake(0, 150, greenView.bounds.size.width, greenView.bounds.size.height);

这不会将框架的原点更改为 (150, 300) 吗?运行上面的代码似乎并没有改变它的框架。 (我知道您不打算使用边界更改 View 位置,这只是一个假设)。

最佳答案

根据 Apple Documentation , 以下是 View 的框架、边界和中心之间的关系:

Although you can change the frame, bounds, and center properties independent of the others, changes to one property affect the others in the following ways:

  • When you set the frame property, the size value in the bounds property changes to match the new size of the frame rectangle. The
    value in the center property similarly changes to match the new
    center point of the frame rectangle.
  • When you set the center property, the origin value in the frame changes accordingly.
  • When you set the size of the bounds property, the size value in the frame property changes to match the new size of the bounds rectangle.

因此,回答您的问题,更改 View 边界上的 X、Y 位置不应影响框架。大多数情况下边界以 (0,0) 开头。将高度或宽度更改为负值将允许边界原点变为负值。

编辑:回答 OP 问题 - 不,更改边界的位置不会以任何方式影响框架。由于边界是引用 View 自身的坐标系,因此在自身坐标系上更改 X、Y 不会改变父 View 坐标系中的位置。

您可以尝试使用两个自定义 View ,如下所示:

UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(50.0f, 100.0f, 150.0f, 150.0f)];
view1.backgroundColor = [UIColor redColor];

NSLog(@"view1.bounds = %@", NSStringFromCGRect(view1.bounds));
NSLog(@"view1.frame = %@", NSStringFromCGRect(view1.frame));

UIView* view2 = [[UIView alloc] initWithFrame:CGRectInset(view1.bounds, 20.0f, 20.0f)];
view2.backgroundColor = [UIColor yellowColor];

NSLog(@"view2.bounds = %@", NSStringFromCGRect(view2.bounds));
NSLog(@"view2.frame = %@", NSStringFromCGRect(view2.frame));

NSLog(@"view1.bounds = %@", NSStringFromCGRect(view1.bounds));
NSLog(@"view1.frame = %@", NSStringFromCGRect(view1.frame));

NSLog(@"view2.bounds = %@", NSStringFromCGRect(view2.bounds));
NSLog(@"view2.frame = %@", NSStringFromCGRect(view2.frame));

[view1 addSubview:view2];

然后像这样更改 subview 绑定(bind):

CGRect frame = view2.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view2.bounds = frame;

改变边界根本不会影响框架。并且这两个 View 在屏幕上看起来是一样的:

enter image description here enter image description here

最后,尝试更改父 View 的边界以查看以下屏幕:

CGRect frame = view1.bounds;
frame.origin.x += 20.0f;
frame.origin.y += 20.0f;
view1.bounds = frame;

enter image description here

关于ios - 更改 View 边界会影响框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161433/

相关文章:

objective-c - drawRect 中的 UILabel 不绘制

ios - 如何将一个单元格加载到另一个单元格的 View 中。?

iframe - Google Analytics、iframe 和跨域

python - 使用 FFMPEG 提取帧?

ios - 从层次结构中删除 View 时,是否调用了 willMoveToSuperview(使用 nil)?

c# - 检查页面是否存在于缓存中,Windows 8 应用程序

ios - Swift:xib文件的继承

ios - Json 数据未显示在 tableView 上

ios - 您如何称呼此iOS 7控件?

ios - UIActivityItemProvider 返回多个对象还是什么都不返回?