I have a List with several text field items and sliders.
For smaller screens, I want to make it scrollable because the content can't fit on the screen. (which is the default behavior).
For larger screens, I want to disable the scrolling behavior, because it can fit on the screen. Scrolling will make slider a bit hard to use.
If the List is scrollable, I want to make the keyboard dismiss mode .onDrag. Otherwise I want to make it .interactive.
How I can do that?
My implementation is simply a standard List, which is under a NavigationView
// under Navigation View
List {
Section {
TextField(..)
Slider(..)
}
Section {
// More Stuff
}
}
.onAppear {
// Need to be .onDrag if scrollable, and .interactive if not
UIScrollView.appearance().keyboardDismissMode = .onDrag
}
I don't like VStack UI because List has nice section UI out of box.
I have seen this question SwfitUI List Make Scrolling disabled but it's not helpful.
Minimal reproducible code:
// This should not be scrollable
struct HomeView {
var body: some View {
List {
Text("foo")
Text("foo")
Text("foo")
Text("foo")
}
}
}
// This should be scrollable
struct HomeView2 {
var body: some View {
List {
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
Text("foo")
}
}
}