skip to Main Content

I have a problem when pushing to a new view with a search bar active.

In the pushed view I want the navigation bar to be hidden. It works in all cases except when the search field is active on the pushing view AND the navigation style is StackNavigationViewStyle.

In the example project below if you select a row, the new view is pushed and the navigation bar is hidden as expected. However, if you first select the search bar to make it active and then press a row, the navigation bar is no longer hidden on the pushed view.

Removing the StackNavigationViewStyle, everything will work fine however I need to have StackNavigationViewStyle.

struct ContentView: View {
    
    @State var searchString = ""
    
    var body: some View {
        NavigationView {
            List {
                NavigationLink {
                    PushedView()
                } label: {
                    Text("Press Me")
                }
            }
            .listStyle(PlainListStyle())
            .searchable(text: $searchString)
            .navigationTitle("First View")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct PushedView: View {
    
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
        
    var body: some View {
        Button {
            self.presentationMode.wrappedValue.dismiss()
        } label: {
            Text("Pop View")
        }
        .navigationTitle("Second View")
        .navigationBarHidden(true)
    }
}

2

Answers


  1. This might be a solution for now:

    import SwiftUI
    
    struct ContentView: View {
        
        @State var searchString = ""
        
        @FocusState private var focusedField: Bool
        @State private var isHidden: Bool = false
        var body: some View {
            NavigationView {
                List {
                    NavigationLink { PushedView(isHidden: $isHidden).onAppear { isHidden = true}.onDisappear {
                        isHidden = false
                    } } label: { Text("Press Me") }
                }
                .navigationTitle("First View")
                .searchable(text: $searchString)
            }
            .navigationViewStyle(StackNavigationViewStyle())
        }
    }
    
    struct PushedView: View {
        
        @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
            
        @Binding var isHidden: Bool
        
        var body: some View {
            Button {
                self.presentationMode.wrappedValue.dismiss()
            } label: {
                Text("Pop View")
            }
            .navigationTitle("Second View")
            .navigationBarHidden(isHidden)
        }
    }
    
    Login or Signup to reply.
  2. I accidentally find a way to make this work and I don’t understand why.

    In PushedView, instead of calling .navigationBarHidden(true), you do this instead:

    • Create a separate state variable and assign value false. Let’s call that variable isBarHidden
    • Call .navigationBarHidden(isBarHidden)
    • In OnAppear, set isBarHidden = true

    The full PushedView should look something like this:

    struct PushedView: View {
        
        @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
        @State private var isBarHidden = false
            
        var body: some View {
            Button {
                self.presentationMode.wrappedValue.dismiss()
            } label: {
                Text("Pop View")
            }
            .navigationTitle("Second View")
            .navigationBarHidden(isBarHidden)
            .onAppear {
                isBarHidden = true
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search