Disable scrolling in SwiftUI ScrollView and List

⋅ 1 min read ⋅ SwiftUI List ScrollView iOS 16

Table of Contents

In iOS 16, SwiftUI finally got a new modifier, scrollDisabled(_:), to disable scrolling in ScrolView and List.

Actually, you can use this modifier to disable scrolling for any scrollable views, such as TextEditor.

How to disable scrolling in ScrollView and List

To disable a scrolling, you put .scrollDisabled(true) to the scrollable view, such as List and ScrollView.

Disable scrolling in ScrollView.

ScrollView {
VStack {
ForEach(0..<7) { i in
Text("Row \(i.description)")
}
}
}
.scrollDisabled(true)

Disable scrolling in List.

List(0..<7) { i in
Text("Row \(i.description)")
}
.scrollDisabled(true)

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

scrollDisabled affects all scrollable views

The .scrollDisabled modifier affects all scrollable views within a view hierarchy.

In the following example, both the ScrollView(.horizontal) and List views are disabled.

List {
ScrollView(.horizontal) {
HStack {
ForEach(0..<7) { i in
Text("Column \(i.description)")
}
}
}
ForEach(0..<7) { i in
Text("Row \(i.description)")
}
}
.scrollDisabled(true)

The bad news is we have no way to enable scrolling of the inner scroll view if the .scrollDisabled is set to true on the parent view.

Setting .scrollDisabled(false) on the inner ScrollView won't work.

List {
ScrollView(.horizontal) {
HStack {
ForEach(0..<7) { i in
Text("Column \(i.description)")
}
}
}
.scrollDisabled(false)
ForEach(0..<7) { i in
Text("Row \(i.description)")
}
}
.scrollDisabled(true)

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Environment Value

SwiftUI passes the disabled state through the environment value, isScrollEnabled.

If you want an inner view to react to this value, you can read it like this.

@Environment(\.isScrollEnabled) private var isScrollEnabled

Read more article about SwiftUI, List, ScrollView, iOS 16, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to show badge on List Row in SwiftUI

Learn how to add and remove a badge on List rows.

Next
Using ForEach in SwiftUI List

You can use ForEach inside a List view in SwiftUI, but when should we use it? Let's learn in which cases we should use ForEach.

← Home