den330 den330 - 1 year ago 33
Swift Question

Bounds and Frame, If a view is Not rotated, is the size of these two property always identical to each other?

I tried to make a view(not rotated) wider in a controller. I tried two approaches to do that:

  1. MyView.frame.size.width += 80

  2. MyView.bounds.size.width += 80

And I noticed that they gave me different effect, how does that happen?

Answer Source

According to the docs on the bounds and frame of a UIView respectively...


Changing the bounds size grows or shrinks the view relative to its center point.


Setting this property changes the point specified by the center property and the size in the bounds rectangle accordingly.

Therefore increasing the bounds width by 80 will add 40pts to the left and right sides of the view, whereas increasing the frame width by 80 will add 80pts to the right hand side of the view.

While you are correct in saying that when the transform is identity, then bounds.size should be the same as frame.size, this doesn't mean that the behaviour when you set these values is the same (which it isn't). I would also take note of what Duncan says about the namings of your variables.