Search code examples
swiftuinavigationbarnavigationview

SwiftUI: Hidden NavigationBar blocks UI


I am using a NavigationView to navigate between views. As I don't want to use the NavigationBar, I am hiding it. Unfortunately, the area where the NavigationBar was (it is hidden) is still blocking the UI. A button there cannot be tapped.

How can I fix this?

// First View
struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                
                Text("Title")
                
                Spacer()
                
                NavigationLink(destination: View2()) {
                    Text("Next")
                }
                .navigationBarTitle("", displayMode: .inline)
                .navigationBarBackButtonHidden(true)
                .navigationBarHidden(true)
                
                Spacer()
            }
        }
    }
}

and

// Second View: Here the button cannot be tapped
struct View2: View {
    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    print("this button doesn't work")
                }, label: {
                    Text("Do something")
                })
                Spacer()
            }
            .padding(.top, 50)
            .edgesIgnoringSafeArea(.all)
        }
        .navigationBarTitle("", displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
    }
}

Solution

  • The issue is due to used second NavigationView - it is wrong, there should be only one root navigation view which manages navigation stack in this case.

    demo

    So here is fixed view (child one)

    struct View2: View {
        var body: some View {
            VStack {
                Button(action: {
                    print("this button doesn't work")
                }, label: {
                    Text("Do something")
                })
                Spacer()
            }
            .padding(.top, 50)
            .edgesIgnoringSafeArea(.all)
            .navigationBarBackButtonHidden(true)
            .navigationBarHidden(true)
        }
    }
    

    Tested with Xcode 13.2 / iOS 15.2

    Note: as you hide navigation but you don't need mode modifier as well, so removed .navigationBarTitle("", displayMode: .inline). Just for your info.