简述
Frame: 视图的方位和巨细运用是父视图的坐标系,所以将视图放置在父级中这一点就很重要。 Bounds:视图的方位和巨细,运用的是其自己的坐标系,而对于这一点而言将视图的内容或子视图放置在其本身内很重要。
frame和bounds
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[superView addSubview:imageView];
Frame origin = (0, 0) width = 100 height = 100
Bounds origin = (0, 0) width = 100 height = 100
该图片中的Frame和Bounds彻底相同。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
[superView addSubview:imageView];
Frame origin = (10, 50) width = 100 height = 100
Bounds origin = (0, 0) width = 100 height = 100
改动Frame的 x、y 坐标会在父视图中移动它。可是视图本身看起来依然彻底相同。Bounds并没有不同。到目前为止,咱们看到的Frame和Bounds的宽度和高度一直是彻底相同的。然而这并不总是正确的。下面看看假如旋转视图片会产生什么。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
[superView addSubview:imageView];
imageView.transform = CGAffineTransformMakeRotation(M_PI * 0.05);
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779618-cb582d74607ceb9.png)
Bounds依然相同,可是Frame已经产生了更改。现在更简单看出frame和bounds之间的差异。
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779624-83a99f9ea159000.png)
Note: When modifying the transform property of your view, all transformations are performed relative to the center point of the view.,修正transform视图的属性时,所有转化都是相对于视图的中心点履行的。也就是说,咱们进行transform后,再去修正视图方位最好运用center属性来修正。
到目前为止,Bounds原点是一直停留在 (0, 0)的,不过也纷歧定是这样的。假如咱们的视图有一个很大的子视图,它太大而无法一次显现呢?
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 120, 200)];
[self.view addSubview:view2];
view2.clipsToBounds = YES;
// view2.bounds = CGRectMake(50, 0, 120, 200);
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(-50, 50, 200, 200)];
[view2 addSubview:imageView2];
[imageView2 setImage:[UIImage imageNamed:@"pic.jpg"]];
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779630-97e6b8680d4358e.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779636-63dadbdee4a65ab.png)
view2.bounds = CGRectMake(50, 0, 120, 200);
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779643-cf5a1ff5ed3fb79.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779649-29e822609ac96c0.png)
UIScrollView系列控件的思维是相同的。大的子视图相当于画布,改动bounds更改的是显现画布的区域。
何时运用Frame,何时运用Bounds
因为
frame相关视图在其父视图中的方位,因而您在进行向外更改时会运用它,例如更改其宽度或查找视图与其父视图顶部之间的间隔。
运用
bounds时,你正在向内改变,就像画的东西或视图中组织子视图。假如您对它进行了一些转化,还能够运用bounds来获取视图的巨细。

![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779601-3328fa951ef9bea.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779606-32d7caf9ddedc9e.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779612-11ccea6aea59ad1.png)