Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Modal Sheet have a Navigation Bar in SwiftUI?

Tags:

ios

swift

swiftui

I am presenting a modal sheet from a navigation bar button in my code:

struct MainPage : View {

    @State var isModalSheetShown: Bool = false

    var body: some View {
        VStack {
            [...]
        }
        .navigationBarItems(trailing: HStack {
            Button(action: { self.isModalSheetShown = true }) {
                Text("Add")
            }
        })
        .sheet(isPresented: $isModalSheetShown, content: {
            VStack {
                [...]
            }
            .navigationBarItems(trailing: HStack {
                Button(action: { ... }) {
                    Text("Done")
                })
            })
        })
    }
}

But the Navigation Bar does not appear in the modal sheet as you can see below.

enter image description here

What am I doing wrong, how do you put a navigation bar on a modal sheet?

like image 898
iSpain17 Avatar asked Jan 12 '20 10:01

iSpain17


People also ask

How do I customize my navigation bar in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.

How does navigation work in SwiftUI?

Before NavigationStack and NavigationSplitView , SwiftUI introduced the NavigationView and NavigationLink structs. These two pieces work together to allow navigation between views in your application. The NavigationView is used to wrap the content of your views, setting them up for subsequent navigation.


2 Answers

You have to wrap your modal view in a NaviagtionView like this

@State var isModalSheetShown: Bool = false

var body: some View {
    VStack {
        Text("Main")
    }
    .navigationBarItems(trailing: Button("Add",
                                         action: { self.isModalSheetShown = true }))
    .sheet(isPresented: $isModalSheetShown) {
        NavigationView {
            VStack {
                Text("Modal")
            }
            .navigationBarItems(trailing: Button("Done",
                                                 action: {}))
        }
    }
}
like image 97
LuLuGaGa Avatar answered Sep 28 '22 01:09

LuLuGaGa


Modal view must be wrapped in NavigationView but the above solution using .navigationBarItems(trailing: Button("Done", action: {})) is not working for me. What worked for me is, in the modal view I have to add a navigationButton and also to show the navigation bar I have to use the .navigationBarTitle("", displayMode: .inline). So this is part of my ModalView code:

  var body: some View {
        VStack (alignment: .leading, spacing: 10) {
            header
            infoBody
            Spacer()
        }
        .padding()
        .navigationBarItems(leading: btnBack)
        .navigationBarTitle("", displayMode: .inline)
    }
like image 32
Mahmud Ahsan Avatar answered Sep 28 '22 00:09

Mahmud Ahsan