我想知道如何正确使用这些属性.

据我所知,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),但是因为view2frame 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中心的位置.

最后,boundsorigin是不相关的概念.两者都允许导出视图的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?.

Ios相关问答推荐

为什么controlSize属性在SwiftUI中不起作用?

使用NavigationStack和.searchable时如何显示大型导航标题?

如何以最可靠的方式删除文本视图中的额外页边距?

如何从ipad获取用户定义的设备名称,IOS版本应大于16在flutter

构建iOS项目失败.我们运行了";xcodeBuild";命令,但它退出并返回错误代码65-expo reaction ative

设置堆栈视图的所有属性

在金属中设置纹理的UV坐标以编程方式变形纹理

NSHashTable要求任何MyCustomProtocolAnyObject都是类类型

如何更改 SwiftUI 弹出视图的大小?

如何在 SwiftUI 中对表格行使用 Transferable

TextField 重复输入 SwiftUI

Swift 共享数据模型在页面之间进行通信.这个怎么运作

将flutter android项目导入iOS

如何将私钥添加到分发证书?

在 Xcode 5 中为超级视图添加间距约束

在 Swift 中追加字符串

如何判断是否为 64 位构建了静态库?

从故事板导航到其他 Swift 1.2 文件时 Xcode 6.3 崩溃

Xcode中的终端窗口?

openURL:在 iOS 10 中已弃用