Swift version: 5.6
All UIView
subclasses have two properties that at first glance seem similar: frame
and 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:
frame
and bounds
are the same.When you change the width or height of either frame
or bounds
, the other value is updated to match. Generally it’s better to modify bounds
plus center
and transform
, and let UIKit calculate the frame
for you.
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 2.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.