How do you show a sheet with a NavigationView in SwiftUI? This is what I tried, but it doesn't show the navigation bar.
struct ContentView: View {
@State var present = false
var body: some View {
VStack {
Button {
present = true
} label: {
Text("Show Sheet")
}
}
.padding()
.sheet(isPresented: $present) {
NavigationView {
Text("Hello World")
}
.navigationTitle("Title")
}
}
}
CodePudding user response:
The .navigationTitle
modifier belongs to the child views of the NavigationView
, so you need to move it inside the closure and on the Text
, which in this case, is the child view.
struct ContentView: View {
@State var present = false
var body: some View {
VStack {
Button {
present = true
} label: {
Text("Show Sheet")
}
}
.padding()
.sheet(isPresented: $present) {
NavigationView {
Text("Hello World")
.navigationTitle("Title") // <-- Here
}
}
}
}