UIScrollView
iOS development
horizontal scrolling
disable scroll
Swift programming

How to disable horizontal scrolling of UIScrollView?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Disabling horizontal scrolling in UIScrollView is usually a layout problem, not a gesture problem. If the scroll view's content width is constrained correctly, horizontal scrolling disappears naturally and vertical scrolling still works. The cleanest fix is to make the content width equal to the scroll view width and avoid fighting the user by resetting offsets manually.

The Right Mental Model

A scroll view scrolls in any direction where its content is larger than its visible bounds. So if horizontal scrolling exists, the content is effectively wider than the scroll view.

That means the best fix is usually:

  • constrain content width to match the scroll view width,
  • allow content height to grow,
  • disable horizontal bounce if needed.

Auto Layout Approach

If you use a content view inside the scroll view, pin it to the content layout guide and lock its width to the frame layout guide.

swift
1import UIKit
2
3final class DemoViewController: UIViewController {
4    let scrollView = UIScrollView()
5    let contentView = UIView()
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9
10        view.addSubview(scrollView)
11        scrollView.translatesAutoresizingMaskIntoConstraints = false
12        NSLayoutConstraint.activate([
13            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
14            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
15            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
16            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
17        ])
18
19        scrollView.addSubview(contentView)
20        contentView.translatesAutoresizingMaskIntoConstraints = false
21        NSLayoutConstraint.activate([
22            contentView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
23            contentView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
24            contentView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
25            contentView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
26            contentView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
27        ])
28    }
29}

That width constraint is the key line. It prevents content from becoming wider than the scroll view.

Helpful Scroll View Settings

You can also reinforce the behavior with configuration.

swift
scrollView.alwaysBounceHorizontal = false
scrollView.showsHorizontalScrollIndicator = false

These do not fix bad layout by themselves, but they are useful when horizontal movement should never be presented to the user.

Avoid The Offset-Reset Hack

Some code samples force contentOffset.x = 0 inside delegate callbacks. That technically blocks horizontal movement, but it creates a worse user experience and can cause jitter.

Conceptually bad pattern:

swift
# if scroll happens:
#   scrollView.contentOffset.x = 0

Use this only as a last resort for legacy layouts you cannot redesign yet.

When contentSize Is Set Manually

If you manage contentSize directly instead of using Auto Layout, make sure width does not exceed bounds width.

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3    let width = scrollView.bounds.width
4    let height: CGFloat = 1200
5    scrollView.contentSize = CGSize(width: width, height: height)
6}

This works, but Auto Layout is easier to maintain in most modern UIKit code.

Table And Collection Views Are Different

If your screen is really a table view or collection view inside a scroll hierarchy, fix the width at the content view level first. Adding more scroll views rarely solves axis-control issues cleanly and often creates nested gesture conflicts.

Nested Views Often Cause The Real Bug

Horizontal scrolling sometimes appears because one child view has an accidental fixed width or ambiguous constraints. If your content width keeps expanding, inspect arranged subviews, labels with no wrapping, and stack views with bad constraints before blaming the scroll view itself.

That is especially common in dynamic forms and long text layouts.

Common Pitfalls

  • Trying to disable horizontal scrolling by resetting offsets instead of fixing layout.
  • Forgetting to lock content width to the scroll view width.
  • Assuming showsHorizontalScrollIndicator = false disables scrolling.
  • Setting contentSize wider than the visible bounds.
  • Ignoring child-view constraints that secretly enlarge the content width.

Summary

  • Horizontal scrolling usually means the content is too wide, not that the scroll view is misbehaving.
  • The clean fix is to constrain content width equal to the scroll view width.
  • Disable horizontal bounce and indicator only as supporting settings.
  • Prefer Auto Layout over manual offset hacks.
  • Debug child constraints if horizontal movement persists unexpectedly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.