How do we get a background image for the ContentView but not its subviews AND retain the behavior of scrollable content disappearing under the NavBar?
I found that if I added the background as a modifier to a List or ScrollView, it didn't change the background on subviews (ex. DetailView) but scrollable content no longer disappeared under the navbar and the NavBar title stayed in place (see pic #2). But if I modified the NavigationView, it restored the behavior of scrollable content disappearing under the navbar (see pic #3) but it also changed the background on all subviews (see pic #4)
I tried a ZStack with Image("MoonPhoto") and then the List
But mostly I tried different placements of this ViewModifier struct:
struct BackgroundImage: ViewModifier {
func body(content: Content) -> some View {
content
.opacity(0.8)
.background(Image("history-in-hd-e5eDHbmHprg-unsplash")
.resizable()
.scaledToFill())
}
}
And here is the main body of ContentView:
var body: some View {
NavigationView {
List(missions) { mission in
NavigationLink(destination: MissionView(mission: mission, astronauts: self.astronauts))
{
Image(mission.image)
.resizable()
.scaledToFit()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text(mission.displayName)
.font(.headline)
Text(self.showCrew ? self.crewList(mission: mission) : mission.formattedLaunchDate)
}
}
.navigationBarTitle("Moonshot")
}
.navigationBarTitle("Moonshot")
.navigationBarItems(leading:
Button(self.showCrew ? "Show Launch date" : "Show crew") {
self.showCrew.toggle()
}
)
.modifier(BackgroundImage()) //Modifying List
} //If you move the above modifier here, the image shows an all subviews too
}
}