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.
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.
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.
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:
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.
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 = falsedisables scrolling. - Setting
contentSizewider 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
- How to disable night mode in my application even if night mode is enable in android 9.0 pie?
- How to disable RecyclerView scrolling?
- how to disable rotation in React Native?
- How to disable scrolling in UITableView table when the content fits on the screen
- How to disable the highlight control state of a UIButton?
- How to disable UITextField editing but still accept touch?
- How to disable/enable the return key in a UITextField?
- How to disable/enable the sleep mode programmatically in iOS?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.