iOS Frame vs Bounds

According to official iOS Documentation about UIView, Frame and Bounds. We could make a cheatsheet below:

  • frame describes the view’s location and size in its superview’s coordinate system.
  • bounds describes the view’s location and size in its own coordinate system. With origin (0, 0) and same size as frame by default.

There is also another saying on internet - bounds is where the subviews are allowed to draw with respect to itself. But it is true only if clipsToBounds is set to TRUE.

In fact, frame, bounds and also center, they are interlocked. Setting one property changes the others in the mean time. Changing the size portion of bounds grows or shrinks the view relative to its center point. Changing the size also changes the size of the rectangle in the frame property to match.

Example

Below is an example to show how bounds effect a view's superview and subview

image.png
// 父 View
let superview: UIView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
superview.backgroundColor = .yellow
        
// 子 View
let childview: UIView = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
childview.backgroundColor = .red
        
// 子 View 的 子 View
let childchildview: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
childchildview.backgroundColor = .green
        
self.view.addSubview(superview)
superview.addSubview(childview)
childview.addSubview(childchildview)
        
// 打印信息
print("superview")
print(superview.frame)  // (100.0, 100.0, 200.0, 200.0)
print(superview.bounds) // (0.0, 0.0, 200.0, 200.0)
print("childview")
print(childview.frame)  // (50.0, 50.0, 100.0, 100.0)
print(childview.bounds) // (0.0, 0.0, 100.0, 100.0)
print("childchildview")
print(childchildview.frame)  // (0.0, 0.0, 50.0, 50.0)
print(childchildview.bounds) // (0.0, 0.0, 50.0, 50.0)

If now we change the origin point of childview's bounds:

childview.bounds = CGRect(x: 100, y: 100, width: 100, height: 100)

So what would happen here is the coordinate system of childview changes. It has no bussiness with its superview but only effects its subview (the "childchildview"). Let's draw below the NEW coordinate system of childview:

new_coordinate_system.png
  • The new coordinate system of childview moves to upper left relative to its old position as origin point of bounds changes from (0, 0) to (100, 100)
  • childview's frame does not change with the respect of its superview, still at (50, 50) in superview's coordinate system.
  • Because the origin point portion of frame of childchildview is at (0, 0), so with respect of its superview - childview, the green rectangle moves also to the new position according to this new coordinate system.

Next, let's set clipsToBounds to true:

childview.clipsToBounds = true

So now, the childchildview is not visible anymore. From now on, we could say bounds is where the subviews are allowed to draw with respect to itself with clipsToBounds set to true.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 其实,很多的时候,好多人都跟我说觉得自己很无情很矫情。可是并不然你更多的时候是太过于珍惜,然后过了界限罢了。 ...
    失忆路人甲yu阅读 142评论 0 0
  • 和LG聊天,告诉他我最近加入了一个写作爱好者的群。每两天就要交1500字以上的文章,若是三次交不上就要被踢出去了。...
    予熙2017阅读 237评论 0 0
  • 1.今日学着用H5,感觉还不错,原来学习一个新东西没有想象中那么难,凡事还是要多试试。 2.学着给自己放假,不管孩...
    凌云仙渡阅读 199评论 0 3