Sobes.tech
Junior — Middle

Are the view sizes obtained through the frame and bounds properties always the same?

sobes.tech AI

Answer from AI

The sizes of a view, obtained through the frame and bounds properties, do not always match.

  • frame is a rectangle describing the position and size of the view in the coordinate system of its superview. The frame.size reflects the width and height of the view in the parent's coordinate system.

  • bounds is a rectangle describing the internal coordinates of the view itself. Usually, bounds.origin is (0,0), and bounds.size is the size of the view in its own coordinate system.

If the view has no transformations (e.g., rotation, scaling), then the sizes of frame.size and bounds.size are the same. But when transformations are applied, the sizes can differ because frame accounts for transformations relative to the superview, while bounds reflects the local size without considering transformations.

Example:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
print(view.frame.size)  // (100.0, 50.0)
print(view.bounds.size) // (100.0, 50.0)

// Apply rotation
view.transform = CGAffineTransform(rotationAngle: .pi / 4)
print(view.frame.size)  // will change, as frame considers the transformation
print(view.bounds.size) // remains (100.0, 50.0)
Are the view sizes obtained through the frame and… - sobes.tech