How to Choose Between frame, bounds, center, and origin in UIKit

Written by

in

Quick answer: Use frame for standard layout relative to superview, center for animations and rotations, bounds for custom drawing regions, and origin only when reading frame’s position. These properties are mathematically linked: frame.origin = center – bounds.size/2.

Why this happens

UIView exposes four geometric properties that overlap in purpose, which creates confusion. frame and bounds both describe a rectangle, but in different coordinate systems. center and frame.origin both describe position, yet they anchor to different points. The properties are not independent—changing one updates the others through fixed mathematical relationships. The key invariant (assuming identity transform) is:

frame.origin = center - bounds.size / 2
center = frame.origin + bounds.size / 2
frame.size = bounds.size

When a view has a non-identity transform (rotation, scale), frame becomes the axis-aligned bounding rectangle in the superview’s coordinates, while bounds and center remain stable in the view’s local space. This is why frame is unreliable during animations that rotate or scale.

How to fix it

1. Use frame for standard view layout

frame is the go-to property for placing a view inside its superview using Auto Layout constraints or manual frame assignment. Its origin is in the superview’s coordinate system, so frame.origin.x and frame.origin.y map directly to left and top edges relative to the container.

// Position a button 20 pts from left, 100 pts from top, size 120x44
button.frame = CGRect(x: 20, y: 100, width: 120, height: 44)

When to use: Initial layout, constraint-based layout fallbacks, hit-testing calculations that need superview coordinates.

2. Use center for animations and rotations

center anchors at the view’s midpoint. Rotations and scales applied via transform happen around this point, so the view stays visually centered while its frame changes. Animating center produces smooth linear motion without the wobble that animating frame.origin can cause when bounds change simultaneously.

// Move view smoothly across screen
UIView.animate(withDuration: 0.5) {
    imageView.center = CGPoint(x: 300, y: 400)
}

// Rotate around center (default behavior)
UIView.animate(withDuration: 0.3) {
    spinner.transform = CGAffineTransform(rotationAngle: .pi)
}

When to use: Sprite-like movement, rotation/scale animations, any case where the visual center should follow a path.

3. Use bounds for custom drawing and clipping

bounds defines the view’s internal coordinate system. Its origin is typically (0,0), and its size matches frame.size. Changing bounds.origin shifts the content drawn inside the view without moving the view itself—this is how UIScrollView implements scrolling. Changing bounds.size resizes the drawable area independently of frame (rare, but useful for custom clipping).

// In a custom UIView subclass
override func draw(_ rect: CGRect) {
    // rect is in bounds coordinates
    let path = UIBezierPath(ovalIn: bounds.insetBy(dx: 10, dy: 10))
    UIColor.systemBlue.setFill()
    path.fill()
}

// Scroll view content offset shifts bounds.origin
scrollView.bounds.origin = CGPoint(x: 0, y: 200)

When to use: draw(_:) implementations, UIScrollView content offset, custom clipping masks, reading UIScreen.main.bounds for screen dimensions.

4. Read origin only when you need frame’s position

origin is not a standalone property—it is the origin component of frame (and bounds). Access it via frame.origin or bounds.origin. Writing view.origin = ... does not exist; you must assign a new frame or bounds rectangle.

// Reading position in superview coordinates
let superviewX = subview.frame.origin.x

// Reading internal drawing origin (usually 0,0 unless scrolled)
let drawingX = subview.bounds.origin.x

When to use: Calculations that need the top-left corner in a specific coordinate space.

How to prevent it

  • Default to frame for layout, center for animation, bounds for drawing.
  • Never animate frame when a transform is applied; animate center and transform instead.
  • Treat bounds.origin as read-only unless you are implementing scrolling or custom coordinate mapping.
  • Remember frame.size == bounds.size always holds; changing one changes the other.
  • Use convert(_:to:) / convert(_:from:) for coordinate-space conversions rather than manual arithmetic.

FAQ

Does changing bounds.size also change frame.size?

Yes. frame.size and bounds.size are always equal. Setting bounds.size = CGSize(width: 200, height: 200) updates frame.size to the same value, and vice versa.

Why does my view jump when I animate frame during a rotation?

During rotation, frame becomes the axis-aligned bounding box of the transformed view, which is larger than the view’s actual dimensions. Animating frame fights the transform. Animate center and transform together instead.

When should I use UIScreen.main.bounds vs. view.window?.windowScene?.screen.bounds?

UIScreen.main.bounds works for single-window iPhone apps. For iPad multitasking, Mac Catalyst, or multi-scene apps, use view.window?.windowScene?.screen.bounds to get the correct screen for that window scene.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.