I can't set a background color under ScrollView
in SwiftUI. When I use .background(Color.red)
the background is cut off so it doesn't go under navigation bar and scrolling seems to be broken.
I tried a couple of solutions but each of them doesn't work.
I have a simple view hierarchy
NavigationView {
ScrollView([.vertical], showsIndicators: true) {
VStack {
ForEach(0...50, id: \.self) { _ in
Text("Text text")
}
}
}
.navigationBarTitle("Title", displayMode: .large)
}
.navigationViewStyle(StackNavigationViewStyle())
it works as expected
now, I would like to add a background color, I tried the following solutions
1
NavigationView {
ScrollView([.vertical], showsIndicators: true) {
VStack {
ForEach(0...50, id: \.self) { _ in
Text("Text text")
}
}
}
.background(Color.red)
.navigationBarTitle("Title", displayMode: .large)
}
.navigationViewStyle(StackNavigationViewStyle())
result
2
NavigationView {
ZStack {
Color.red
ScrollView([.vertical], showsIndicators: true) {
VStack {
ForEach(0...50, id: \.self) { _ in
Text("Text text")
}
}
}
.navigationBarTitle("Title", displayMode: .large)
}
}
.navigationViewStyle(StackNavigationViewStyle())
result
3
NavigationView {
ZStack {
Color.red.edgesIgnoringSafeArea([.all])
ScrollView([.vertical], showsIndicators: true) {
VStack {
ForEach(0...50, id: \.self) { _ in
Text("Text text")
}
}
}
.navigationBarTitle("Title", displayMode: .large)
}
}
.navigationViewStyle(StackNavigationViewStyle())
result
How to set up a background color under ScrollView
packed in NavigationView
?
// edit:
The animation below presents a desirable effect (it is made with UIKit).