Member-only story
What Are Content Hugging and Compression Resistance in Swift
Make your Auto Layout more adaptive and efficient
In this article, we will learn how to make our UI layout more adaptive by utilizing content hugging and compression resistance properties Auto Layout offers. In short, this is what you will have learned by the end of the tutorial:
- How to create an adaptive layout programmatically.
- How to leverage content hugging to help Auto Layout decide what view to expand.
- How compression resistance makes it easier to shrink a view beyond its intrinsic size.
The source code of the sample project is available at the bottom of the article.
Let’s Start
We start with an almost empty UITableViewController
subclass:
The TableViewCell
has two labels positioned next to each other, using the following code: