Berry Blue
Berry Blue

Reputation: 16542

How to show a sheet with a NavigationView in SwiftUI

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")
        }
    }
}

Upvotes: 1

Views: 1788

Answers (1)

jnpdx
jnpdx

Reputation: 52625

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
            }
        }
    }
}

Upvotes: 3

Related Questions