Better Programming

Advice for programmers.

Follow publication

Member-only story

What Are Content Hugging and Compression Resistance in Swift

Make your Auto Layout more adaptive and efficient

Zafar Ivaev
Better Programming
Published in
5 min readOct 11, 2021
Apple laptop
Photo by Mohammadreza alidoost on Unsplash

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:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Zafar Ivaev
Zafar Ivaev

Written by Zafar Ivaev

Senior iOS Engineer. I write about features of Swift and iOS development practices.

Responses (1)

Write a response