Frame vs Bounds in iOS: Implementing A Visual Demonstration

Updated on August 28th, 2023

⏱ Reading Time: 8 mins

There is an all-time popular and common interview-related topic among iOS, and not only, developers; the difference between frame and bounds of a view! Even though all developers get to know that sooner or later, it might sound the same to the ears of real beginners. Disregarding to look deeper at that difference, it’s almost always guaranteed that troubles and confusion are ahead at some point. Therefore, it’s for the best interest of every developer to get that out of their way as soon as possible.

My purpose in this post is to talk about that difference. However, instead of just saying a couple of words explaining what the frame and bounds are, I will also proceed to an additional step; to implement step by step a small iOS app that will demonstrate visually these two concepts. That way, it’s going to be absolutely clear what they are all about, making any kind of confusion disappear.

Knowing the basics

First things first, so let’s get started with a couple of explanations. Both frame and bounds describe two things in a view:

  • the origin point of the view as a CGPoint value (x and y values on horizontal and vertical axises),
  • the size of the view as a CGSize value (width and height).

But, if both of them provide similar kind of information, where do they really differ then? ????

Well, the difference lies to the reference coordinate system. Specifically:

  • Frame refers to the coordinate system of the view’s container (parent view).
  • Bounds refer to the own coordinate system of the view.

For instance, suppose that a view controller contains a single view (a subview) only, besides its own view. The frame of that subview describes its origin point and size in the coordinates of the view controller’s view, as the latter is the container (parent) view in this case. The zero origin point (x=0 and y=0) of the container is on the top-left side, and the subview’s origin is the distance from that point in both axises. Regarding the size of the subview, that is the width and height of the virtual rectangle that surrounds the subview at any given moment.

When it comes to bounds, the origin point is the top-left side of the subview itself, which in its own coordinate system is always equal to zero (0, 0). The width and height express the actual size of the view, and that remains always constant, regardless of any transformation that might have been applied to the view. More about that in a while.

We’ll make all that totally clear right next. Before we get there, the following image illustrates the origin point of both the parent view and the subview as described above:

???? Note: The origin point is the top-left corner in iOS, but this is not the case in macOS; the origin point is the bottom-left corner.

Based on all the above, I think it’s obvious that the origin point value is the first difference between frame and bounds. But that’s not the only one; the size can also be different too!

By default, the size of a view is the same in both frame and bounds. However, this statement does not remain valid if we transform the view in some way; rotation, translation (move) and scale, they all affect the frame; both origin point and size!

And this is where real problems usually start. If there are UI-related calculations based on the frame of one or more views, and any of those views get transformed, calculations will be wrong because the size is altered. Unless of course this is done intentionally. If not, there will be awkward visual results, which in turn lead to headaches while trying to figure out why the interface does not behave as it was originally supposed to.

That said, it’s time to go for a quick implementation that will clarify the previously mentioned differences between frame and bounds.

Implementing the demo

In Xcode, and in a brand new UIKit-based iOS app, we’ll implement the following:

  • A simple view that we’ll use as demo in order to demonstrate its frame and bounds.
  • Another view that will be surrounding the first one, with purpose to be showing visually the actual frame of the demo view at any given moment.
  • A third view that will be reflecting the demo view’s bounds.
  • A button that will be rotating the first view a few degrees each time.
  • Two labels which will be displaying the values of both the frame and bounds of the first view.

All the UI implementation will be done programmatically; no use of the storyboard. So, let’s begin in the default view controller that every new UIKit project contains, where we’ll declare the few properties we are going to need:

In addition to these, we’ll need one more property in order to keep the current rotation (in degrees) of the demo view:

Next, we’re going to define a few methods in the ViewController class. Most of them will be configuring the controls that we just declared right above.

The first method we’ll focus on regards the initialization and configuration of the demo view. As you will see next, we give it both a background and a border color in order to make it visually distinguishable and prominent on screen.

After that, we are going to initialize and configure the frameView that will be surrounding the demo view. There will be two differences here; the frame view will have only a colored border, and most importantly, we’ll set no layout constraints to it! Instead, we’ll be setting its frame on the fly each time the frame of the demo view gets changed:

Quite similarly, we’ll initialize and configure the boundsView. Our goal with this view is to visually represent the bounds of the demo view, and we’ll achieve that by setting the demo view’s bounds as the frame of the boundsView. Once again, we won’t set any auto layout constraints for this view either.

Right after all the above, we’ll add a button. The purpose of its existence is simple; every time that we’ll be tapping on it, we’ll be changing the transformation of the demo view by rotating it a few degrees. There is nothing particularly difficult in this method, so here is its implementation:

We’ll define the transform() method that is being called in the button’s action closure pretty soon; don’t bother about it at the moment.

???? Note: If you want to find out about more buttons with action closures, take a look at this post.

The last views remaining to be configured are the two labels that will be reporting the frame and bounds values of the demo view. Their initialization and configuration takes place in two different methods, but as you will see next, there are two more methods defined as well. The first one implements common properties between the two labels, and the other implements common auto layout constraints. Doing so helps avoid repeating the same code twice:

The final touches

Having finished at this point with the configuration of all views in this small demo app, it’s time to add three more missing methods. We’ll begin with two similar methods, which are going to be responsible for setting the frame and bounds values of the demo view as the text of the two labels. However, for your own convenience, we’ll also make them print these values on the console too:

Next, we’ll implement the transform() method that we first met previously, and it will be called every time the rotate button is tapped. There are three important things happening in it:

  • the demo view is rotated by 15 degrees each time,
  • the frameView gets a new frame; the frame of the demo view,
  • the boundsView also gets a new frame; the bounds of the demo view.

In addition, we’ll also call the printFrame() and printBounds() methods in order to update the content of the two labels.

Here’s all that:

Finally, let’s have everything laid out on screen, and let’s have the first content of the two labels displayed; we’ll do all that in the viewWillAppear(_:) method:

Witnessing frame and bounds differences

The small app of this tutorial is ready, so it’s time to try it out. Run it either in a real device, or in the simulator, and when ready start tapping on the rotate button to rotate the demo view.

Here is what you will see happening:

While the demo view rotates, notice that:

  • the border of the frameView is “drawn” around the virtual rectangle of the demo view. What you see is the real frame of the demo view after rotation!
  • the boundsView is showing up where its frame dictates, which is equal to the demo view’s bounds. We clearly realize that while rotating the demo view, the bounds view remains constant all the time.

The two labels at the bottom of the screen also state that. With every rotation, the origin point, as well as the size of the demo view’s frame get new values that reflect the view’s position and dimensions. However, the respective bounds values remain constantly unchanged, as we can see at the second label on the right side!

Don’t be surprised by seeing the borderView with the green border color to be positioned on the top-left corner of the screen. That’s something that we should be expecting, as the origin value of its frame matches to the origin point of the demoView bounds, which is equal to (0, 0).

Even though in this post we changed the demo view’s transformation just by rotating it, the results are similar with other transformation as well, such as scale or translate. In all cases, the original frame of the demo view is altered according to the applied transformation.

Conclusion

Knowing the difference between frame and bounds of a view can really help avoid problems with the user interface we are building. Sometimes it might be desirable to have other views change accordingly to the changes applied to the frame of a view, but usually that’s an unwanted effect. Keep what you’ve just read here in consideration when accessing the origin point or the size of a view, and choose wisely if you’ll do that through the frame or the bounds.

Thank you for reading, enjoy coding! ????

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.