Illustration created by author using ChatGPT-4o and DALL-E

Adaptive UI Layouts in Swift: Understanding Content Hugging and Content Compression Resistance

Fine-Tuning Interface Elements for Seamless User Experience Across Devices

Cong Le
Published in
9 min readFeb 2, 2024

--

Last Updated: August 20, 2024

As an iOS developer who has navigated the complexities of interface design, I’ve come to appreciate the power of Auto Layout. Creating adaptive user interfaces that gracefully respond to changes like screen size, device orientation, and localization is no small feat.

Auto Layout is the backbone of this adaptability, and today, I want to dive into two critical concepts: Content Hugging and Content Compression Resistance. These are essential tools in our toolkit for ensuring user-friendly layouts.

Auto Layout:

  • Auto Layout is a framework in iOS development.
  • Allows developers to define relationships between UI elements using constraints.
  • Enables dynamic adjustment of UI elements based on varying conditions such as screen size and orientation.
  • Provides the capability to create interfaces that adapt to different device configurations and localization settings.
  • Facilitates the creation of consistent and responsive layouts across multiple screen sizes.

Methods to Achieve Auto Layout in Swift with Xcode IDE

  1. Interface Builder (Storyboard/XIB Files): I often use Interface Builder for a visual approach to setting up Auto Layout constraints. It’s intuitive and lets you see the changes in real-time.
  2. Programmatic Auto Layout: For more dynamic and complex layouts, defining UI elements and constraints programmatically in Swift code is my go-to method. It offers flexibility and control that can be crucial in large projects.
  3. SwiftUI: With SwiftUI, we get the benefit of a declarative syntax that handles many layout aspects for us, including Content Hugging and Content Compression Resistance. It adapts automatically, which is a huge time-saver.

Choosing the Programmatic Method for Content Hugging and Compression Resistance

In my experience when working within a large team, I prefer the programmatic method for implementing Content Hugging and Content Compression Resistance in UIKit. Here’s why:

  • Scalability: Programmatic constraints make the codebase scalable. It’s easier to adapt to changes or add new features without reworking large parts of the UI.
  • Reusability: Once written, these constraints can be reused across different sections of the app or even in other projects. This promotes consistency and efficiency.
  • Version Control: Managing layouts in code is much cleaner in version control systems. We avoid the headaches that come with binary storyboard files.
  • Collaboration: Code-based approaches make it easier for team members to collaborate, review, and merge changes seamlessly.

Content Hugging:

  • Content Hugging defines how strongly a view resists growing larger than its intrinsic content size.
  • Views with higher content hugging priority will be less likely to expand to fill extra space in a layout.
  • It’s used when you want a view to be just big enough to fit its content, and not any larger.
  • Example: A label with a short text string will not stretch beyond the text’s width if it has a high content-hugging priority.

Content Compression Resistance:

  • Content Compression Resistance defines how strongly a view resists shrinking smaller than its intrinsic content size.
  • Views with higher compression resistance priority will be less likely to shrink and truncate their content.
  • It’s used to ensure that a view’s content remains fully visible even when space is constrained.
  • Example: A button with a title will maintain its width to prevent the title from being clipped if it has a high content compression resistance priority.

Practical Implementation in Swift

Additional setup for XCode

To bridge the gap between theory and practice, let’s explore how to apply the concepts of Content Hugging and Content Compression Resistance within Xcode. This requires setting up our project to seamlessly integrate SwiftUI and UIKit frameworks. For a detailed walkthrough, I’ve prepared a step-by-step tutorial that you can access here:

However, if you’re eager to jump straight into implementation, feel free to skip the tutorial and download the pre-configured Swift project from my GitHub repository:

This will allow you to immediately begin exploring Content Hugging and Content Compression Resistance as we dig deeper into their practical applications in the following sections.

Horizontal Stack View

To demonstrate these concepts in a real-world scenario, let’s create a DemoAutoLayoutViewControllerclass in Swift with two helper functions: setupVerticalStackLayout and setupHorizontalStackLayout.

////  DemoAutoLayoutViewController.swift
import UIKit

class DemoAutoLayoutViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.setupHorizontalStackLayout()
// self.setupVerticalStackLayout()
}
}

The setupHorizontalStackLayout function showcases how we can manage the layout of two buttons within a horizontal stack view:

////  DemoAutoLayoutViewController.swift
//...
//...


/// Sets up a horizontal stack view containing two buttons and demonstrates how content hugging and content compression resistance priorities affect their layout.
///
/// This function creates two buttons, adds them to a horizontal stack view, and sets up the stack view's layout constraints.
/// It shows how to control the layout behavior of the buttons by setting their content hugging and content compression resistance priorities.
func setupHorizontalStackLayout() {
// Create two buttons
let button1 = UIButton(type: .system)
button1.setTitle("Button 1", for: .normal)
button1.backgroundColor = .lightGray

let button2 = UIButton(type: .system)
button2.setTitle("Button 2", for: .normal)
button2.backgroundColor = .lightGray

// Create a horizontal stack view
let stackView = UIStackView(arrangedSubviews: [button1, button2])
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false

// Add the stack view to the main view
view.addSubview(stackView)

// Set up constraints for the stack view
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
])

// Demonstrate content hugging and content compression resistance
self.setHighPriorityHuggingAndCompression(button1: button1, button2: button2)
// self.setLowPriorityHuggingAndCompression(button1: button1, button2: button2)
}

/// Sets low content hugging and low content compression resistance priorities for the buttons in the horizontal stack view.
///
/// This configuration allows the first button (firstButton) to stretch horizontally if needed and allows the second button (secondButton) to compress horizontally if needed.
///
/// - Parameters:
/// - firstButton: The first button in the horizontal stack view. It can stretch horizontally if needed.
/// - secondButton: The second button in the horizontal stack view. It can compress horizontally if needed.
func setLowPriorityHuggingAndCompression(button1: UIButton, button2: UIButton) {
button1.setContentHuggingPriority(UILayoutPriority.defaultLow, for: .horizontal)
button2.setContentCompressionResistancePriority(UILayoutPriority.defaultLow, for: .horizontal)
}


/// Sets high content hugging and high content compression resistance priorities for the buttons in the horizontal stack view.
///
/// This configuration makes the first button (firstButton) resist stretching horizontally and the second button (secondButton) resist compressing horizontally.
///
/// - Parameters:
/// - firstButton: The first button in the horizontal stack view. It resists stretching horizontally.
/// - secondButton: The second button in the horizontal stack view. It resists compressing horizontally.
func setHighPriorityHuggingAndCompression(button1: UIButton, button2: UIButton) {
button1.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .horizontal)
button2.setContentCompressionResistancePriority(UILayoutPriority.defaultHigh, for: .horizontal)
}

You need to update the UIKitViewControllerWrapperclass to load our DemoAutoLayoutViewController from the UIKit framework. To do this, change the return type ofmakeUIViewControllerandupdateUIViewController to DemoAutoLayoutViewController, as shown in the following code:

struct UIKitViewControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = DemoAutoLayoutViewController

// Step 1b: Required methods implementation
func makeUIViewController(context: Context) -> DemoAutoLayoutViewController {
// Step 1c: Instantiate and return the UIKit view controller
return DemoAutoLayoutViewController()
}

func updateUIViewController(_ uiViewController: DemoAutoLayoutViewController, context: Context) {
// Update the view controller if needed
}
}

At this point, your completed UIKitViewControllerWrapper.swift file should look like below:

Now, open theContentViewfile to see the content of the DemoAutoLayoutViewController class from the UIKit framework rendered inside a SwiftUI view on the Xcode canvas, as shown in the screenshot below:

Preview of 2 buttons with function setHighPriorityHuggingAndCompression is enable.
Preview of 2 buttons with function setLowPriorityHuggingAndCompression is enable.

Congratulations! You are now proficient in applying content hugging and content compression resistance concepts to create a horizontal stack layout.

Vertical stack view

Conversely, thesetupVerticalStackLayoutfunction demonstrates how to manage the layout of two labels within a vertical stack view in the DemoAutoLayoutViewController.swiftfile:

//// DemoAutoLayoutViewController.swift

/// Sets up a vertical stack view containing two labels and demonstrates how content hugging and content compression resistance priorities affect their layout.
///
/// This function creates two labels, adds them to a vertical stack view, and sets up the stack view's layout constraints.
/// It shows how to control the layout behavior of the labels by setting their content hugging and content compression resistance priorities.
func setupVerticalStackLayout() {
// Create two labels
let label1 = UILabel()
label1.text = "Label 1"
label1.backgroundColor = .lightGray
label1.textAlignment = .center

let label2 = UILabel()
label2.text = "Label 2"
label2.backgroundColor = .lightGray
label2.textAlignment = .center

// Create a vertical stack view
let stackView = UIStackView(arrangedSubviews: [label1, label2])
stackView.axis = .vertical
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false

// Add the stack view to the main view
self.view.addSubview(stackView)

// Set up constraints for the stack view
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
stackView.heightAnchor.constraint(equalToConstant: 200)
])

// Demonstrate content hugging and content compression resistance
self.setStretchAndPreventCompression(label1: label1, label2: label2)
//self.setPreventStretchAndAllowCompression(label1: label1, label2: label2)
}

/// Sets low content hugging and high content compression resistance priorities for the labels in the vertical stack view.
///
/// This configuration allows the first label (firstLabel) to stretch vertically if needed and ensures the second label (secondLabel) resists compressing vertically.
///
/// - Parameters:
/// - firstLabel: The first label in the vertical stack view. It can stretch vertically if needed.
/// - secondLabel: The second label in the vertical stack view. It resists compressing vertically.
func setStretchAndPreventCompression(label1: UILabel, label2: UILabel) {
label1.setContentHuggingPriority(UILayoutPriority.defaultLow, for: .vertical)
label2.setContentCompressionResistancePriority(UILayoutPriority.defaultHigh, for: .vertical)
}


/// Sets high content hugging and low content compression resistance priorities for the labels in the vertical stack view.
///
/// This configuration ensures the first label (firstLabel) resists stretching vertically and allows the second label (secondLabel) to compress vertically if needed.
///
/// - Parameters:
/// - firstLabel: The first label in the vertical stack view. It resists stretching vertically.
/// - secondLabel: The second label in the vertical stack view. It can compress vertically if needed.
func setPreventStretchAndAllowCompression(label1: UILabel, label2: UILabel) {
label1.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .vertical)
label2.setContentCompressionResistancePriority(UILayoutPriority.defaultLow, for: .vertical)
}

Similar to the steps used for creating a horizontal stack layout, you can apply the same principles to a vertical stack layout to observe the effects of content hugging and content compression resistance.

If set up correctly, you will see the following screenshot displaying your vertical stack layout with two labels.

Preview of 2 buttons with function setStretchAndPreventCompression is enable.
Preview of 2 buttons with function setPreventStretchAndAllowCompression is enable.

Conclusion

Content Hugging and Content Compression Resistance are indispensable for controlling view behavior when space is tight or plentiful. While UIKit requires us to implement these manually, SwiftUI handles them automatically, freeing us to focus on other aspects of development. These concepts ensure our layouts are consistent and user-friendly across all screen sizes.

As you continue to explore and refine your Auto Layout skills, consider this: What are some of the challenges you’ve faced while implementing Content Hugging and Compression Resistance, and how have you overcome them?

I encourage you to share your experiences and insights in the comments below, fostering a collaborative learning environment for the iOS development community.

These official Apple documentations will provide you with detailed technical guidance and examples to effectively use Auto Layout in your iOS development projects:

Stay in the loop!

Subscribe to my email newsletter and I’ll notify you whenever I publish a new article.

➡️ [Click here to subscribe]

If you found this article helpful, would you mind showing your support by following me and clapping a few times? Or feel free to buy me a beer 🍺 🤪. It really helps! Thanks for reading!

--

--

Cong Le
Swift And Beyond

iOS Developer | Exploring Stable Diffusion, LLMs, and Al-powered video/audio generation for mobile apps.