Do you know what translatesAutoresizing MaskIntoConstraints actually does? đ¤¨
Hi đ
Now that Iâm back home from all the traveling I did in January, Iâve been spending a bit of time working on my video setup đď¸
More specifically Iâve been focusing on creating a nice lighting ambiance that would also match the colors Xcode uses for syntax coloring!
Last Thursday I did my first livestream with this new color palette, and I must that Iâm pretty happy with the result đ
That being said, how about we jump into the topic of this email?
This week I want to talk about a line of code that probably caused you some trouble at one point or another:
If youâve ever written programmatic layout code with UIKit, you already know that if you forget this line, itâs guaranteed that your UI will break.
But have you ever wondered why thatâs the case?
What does this line exactly do?
And whatâs this âautoresizing maskâ that it mentions?
A good way to start answering these questions is to actually look at the documentation of the property translatesAutoresizingMaskIntoConstraints
:
In this documentation, we learn several things.
If this propertyâs value is true, the system creates a set of constraints that duplicate the behavior specified by the viewâs autoresizing mask.
So when the property is set to true
, a set of constraints is automatically added to the view, in addition to the ones we will also add ourselves.
Note that the autoresizing mask constraints fully specify the viewâs size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts.
We also lean that these constraints will completely specify the viewâs coordinates. So if we add our own constraints on top, we are indeed doomed to create conflicts that will break the layout of our UI.
By default, the property is set to true for any view you programmatically create. If you add views in Interface Builder, the system automatically sets this property to false.
Finally, we learn that all views donât behave the same way when it comes to translatesAutoresizingMaskIntoConstraints
:
views created in Interface Builder (i.e. in a .xib or a .storyboard file) will have the property set to
false
by defaultviews created in code will have the property set to true by default
But why is the property set to true
by default for views created programmatically?
To understand that, we need to dig a bit deeper and understand what this âautoresizing maskâ actually is.
If, like me, youâre an old time iOS developer, you probably remember that there was a time when AutoLayout didnât exist on iOS.
And the reason was simple: it simply wasnât needed!
You see, until the release of the iPhone 5, all the iPhones (and all the iPads) actually had the same screen aspect ratio.
This meant that it was perfectly ok to directly hardcode the coordinates of each viewâs frame.
However, we still needed a way to handle simple resizing, so that apps could be displayed both in portrait and landscape mode.
And thatâs when these âautoresizing masksâ came into play!
An autoresizing mask offers a simple API to describe how a child view should respond to changes in the size of its parent view.
Namely, the autoresizing mask of a view allows you to specify whether or not the viewâs height, width, top, left, right and bottom margins should change when its parent view resizes.
The UI to define an autoresizing mask is actually still present in Xcode!
You might have seen it before without realizing what it was meant for:
Alright, weâve now covered all the information we need to answer our initial question:
What does translatesAutoresizingMaskIntoConstraints
actually do?
translatesAutoresizingMaskIntoConstraints
is a property of UIView
that enables developers to emulate the first layout system used by iOS into the more recent AutoLayout system.
In order not to break UI code that predated AutoLayout, iOS decided that this property would be set to true by default when a UIView is initialized programmatically.
However, when we want to add our own set of AutoLayout constraints to a UIView
, itâs very important that we manually set the property back to false
, otherwise the two set of constraints will conflict with each other đ
I hope youâve enjoyed this deep dive into this very famous but also very puzzling line of UIKit code!
If you want to learn in more details how autoresizing masks work and how they compare to autolayout constraints, you can watch the replay of the livestream I did on that topic last week!
Thatâs all for this email, thanks for reading it!
If youâve enjoyed it, feel free to forward it
to your friends and colleagues đ
I wish you an amazing week!
â¤ď¸