Swift version: 5.1
UIView subclasses have two properties that at first glance seem similar:
bounds. Both return a
CGRect – a rectangle containing their X and Y position, plus their width and height – but that doesn’t mean they are the same.
At its simplest, a view’s bounds refers to its coordinates relative to its own space (as if the rest of your view hierarchy didn’t exist), whereas its frame refers to its coordinates relative to its parent’s space.
This means a few things:
boundsare the same.
When you change the width or height of either
bounds, the other value is updated to match. Generally it’s better to modify
transform, and let UIKit calculate the
frame for you.
SAVE 20% ON iOS CONF SG The largest iOS conference in Southeast Asia is back in Singapore for the 5th time in January 2020, now with two days of workshops plus two days of talks on SwiftUI, Combine, GraphQL, and more! Save a massive 20% on your tickets by clicking on this link.
Available from iOS 2.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.