This workshop will be retired on May 31, 2020.
When setting up views, there are two coordinate systems we often use. In this video, let's let's learn about frames and bounds, and why they are important in Swift programming hierarchy.
Apple View Programming Guide for iOS
iOS Core-Graphic & Core-Image
Frame vs Bounds discussion from Stack Overflow
Updated Code Samples
Before we go any further, 0:00 it's time to discuss what the differences between frame and bounds. 0:02 The short answer to the question is that the bounds of a UI view is the rectangle 0:08 expressed as a location xy, and size within height relative 0:13 to its own coordinate system The frame, on the other hand, is a rectangle, as well. 0:19 Again, expressed as a location xy in size, width, and height. 0:24 But this time, relative to the super view it is contained within. 0:28 Though these are precise and succinct definitions, 0:33 if you're not familiar with view geometry, they might seem a bit cryptic. 0:36 A longer explanation, one that should make these definitions a bit more meaningful, 0:42 needs to start with the reason why there needs to be 0:47 two ways of specifying the rectangle that defines a UI view. 0:50 This need arises from the system that makes 0:55 programming a complex view hierarchy manageable. 0:58 For a concrete example of the potential complexity inherent in a view hierarchy, 1:02 imagine you have a device running a simple app. 1:08 Lets imagine that it is a UIview with a couple of subviews. 1:11 Then suppose your app displays a pop-up which is a view and 1:16 in this pop-up there's a button which is also a view. 1:20 What makes programming the buttons layout inside the popup manageable 1:24 is the buttons ignorance of all other views in the hierarchy. 1:28 If views were required to understand their layouts 1:34 in terms of device screen coordinates in order to join the view hierarchy, 1:37 then they would be required to know of all the layouts of all their superviews. 1:42 Needing to address this complexity, 1:48 within each view would make constructing view hierarchies nearly impossible. 1:49 Thankfully, in our example, 1:55 the hypothetical button only needs to understand its layout within the popup. 1:57 And in turn the popup only needs to know how it sits in its superview. 2:02 Each view provides a simple limited world for it sub views 2:07 that are given to believe that the superview sits at coordinates zero zero. 2:11 This simplified reality that a view shares with its sub views is called the views 2:16 bounds. 2:21 The frame is where the sub view thinks it 2:22 exists in terms of the bounds provided by its parent. 2:25 The frame is the reality of viewshares with its superview 2:30 while the bounds is the reality it presents to it subviews. 2:34 And this is a complicated topic. 2:39 So don't worry if it doesn't make complete sense immediately. 2:40 More information about frame and balance can be found in the teacher's notes. 2:44
You need to sign up for Treehouse in order to download course files.Sign up