I'm trying to create a ScrollView
with a background colour inside a NavigationView
, and running into some problems. Here's the code:
struct MainView: View {
var body: some View {
NavigationView {
ScrollView {
VStack {
ForEach(0..<20) { i in
HStack {
Spacer()
Text("Row \(i)")
Spacer()
}
.padding()
.background(Capsule().foregroundColor(.white))
.padding()
}
}
}
.navigationBarTitle("Main View")
// Line A:
.background(Color.blue.edgesIgnoringSafeArea(.all))
}
}
}
Notice the line I've marked as 'Line A'. Without this line, the title "Main View" appears as a large title at the top of the screen, then dynamically changes to the smaller, inline version when I scroll down (as expected). However, when I add Line A, this scrolling behaviour no longer works as expected; when I scroll down, the contents of the ScrollView simply slide under the large title, and the large title no longer changes to an inline version.
I've tried using a ZStack as an alternative method of adding the background color, but this results in the same problem.
I'd be hugely grateful if anyone could explain what's going wrong here and/or suggest a solution!