UIScrollView
contentInset
iOS development
Swift
app development

What's the UIScrollView contentInset property for?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

contentInset adds padding around the scrollable content inside a UIScrollView. That sounds simple, but it is one of the most useful layout tools in UIKit because it changes where content can scroll, where it visually starts and ends, and how the scroll view behaves around bars, keyboards, and safe areas.

What contentInset Actually Changes

A UIScrollView has a content area defined by its contentSize. The contentInset expands that scrollable area by adding extra space on the top, left, bottom, or right.

Example:

swift
scrollView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 40, right: 0)

This does not resize the subviews inside the scroll view. Instead, it changes the padding around them from the scroll view's perspective.

That affects:

  • how far content can be scrolled
  • where the content appears to begin and end
  • how much space is preserved around overlays such as toolbars or keyboards

A Simple Mental Model

Think of contentInset as extra scrollable margin. If the top inset is 20, the content can sit 20 points lower than the raw top edge. If the bottom inset is 40, the user can scroll so there is 40 points of empty space below the last content item.

This is why it is useful for forms, chat screens, and lists that would otherwise be hidden behind UI chrome.

Common Use Case: Keep Content Visible Above the Keyboard

A classic example is increasing the bottom inset when the keyboard appears.

swift
1import UIKit
2
3final class FormViewController: UIViewController {
4    @IBOutlet private weak var scrollView: UIScrollView!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        NotificationCenter.default.addObserver(
10            self,
11            selector: #selector(handleKeyboard),
12            name: UIResponder.keyboardWillChangeFrameNotification,
13            object: nil
14        )
15    }
16
17    @objc private func handleKeyboard(_ notification: Notification) {
18        guard
19            let userInfo = notification.userInfo,
20            let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
21        else {
22            return
23        }
24
25        let localFrame = view.convert(keyboardFrame, from: nil)
26        let overlap = max(0, view.bounds.maxY - localFrame.minY)
27
28        scrollView.contentInset.bottom = overlap
29        scrollView.scrollIndicatorInsets.bottom = overlap
30    }
31}

Here the bottom inset creates space so the user can still scroll the active text field above the keyboard.

contentInset Versus adjustedContentInset

On modern iOS, safe areas and container controllers can automatically affect a scroll view. That is why adjustedContentInset exists.

  • 'contentInset is the value you set directly'
  • 'adjustedContentInset is the effective inset after UIKit adds automatic adjustments such as safe-area handling'

You often read adjustedContentInset when debugging why the visible padding is larger than the value you explicitly assigned.

contentInset and contentOffset Work Together

Insets affect how offsets feel.

For example, if you give a scroll view a top inset of 100, the visual resting position can show content lower than the physical top of the content area. That means contentOffset values should be interpreted alongside the inset.

A small example:

swift
scrollView.contentInset.top = 60
scrollView.setContentOffset(CGPoint(x: 0, y: -60), animated: false)

That offset is often used when you want the content to appear naturally aligned below a padded header region.

Useful for More Than Obstruction Avoidance

Developers often learn contentInset only as a keyboard workaround, but it is also good for intentional layout.

Examples:

  • adding breathing room above the first item in a feed
  • preserving space for a floating button or toolbar
  • making the last cell scroll above a bottom overlay
  • creating card-like padding around a long form

In all of these cases, the content itself stays the same size. The scrollable padding changes.

A Table View Example

Because UITableView is a scroll view subclass, the same property applies there too.

swift
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 80, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset

This is a common way to keep the last rows from disappearing behind a persistent control near the bottom of the screen.

Common Pitfalls

The biggest mistake is expecting contentInset to resize or reposition the subviews inside the scroll view. It does not change the actual content layout; it changes the scrollable padding around that layout.

Another issue is forgetting about adjustedContentInset. If safe-area adjustment is active, the visible result may not match the raw value you set on contentInset.

Developers also update the inset for the keyboard but forget scrollIndicatorInsets, which leaves the scroll bars overlapping the keyboard or bottom controls.

Finally, do not pile on inset values blindly. If multiple parts of the code all modify the bottom inset independently, the final scroll behavior becomes hard to reason about.

Summary

  • 'contentInset adds padding around a scroll view's content.'
  • It changes scrollable space, not the size of the content itself.
  • It is commonly used for keyboards, bars, and intentional visual spacing.
  • 'adjustedContentInset includes automatic system adjustments such as safe-area effects.'
  • When using inset changes for visibility, update scrollIndicatorInsets as well.

Course illustration
Course illustration

All Rights Reserved.