skip to Main Content

I’m writing a fairly simple SwiftUI app about movies and I have this issue where the new .searchable modifier on NavigationView is always being shown, whereas it should be hidden, unless you pull down on the List.

It hides it correctly if I scroll a bit up, and if I scroll down it also hides it correctly, but other than that, it’s always being shown. See gif for clarification. (basically it should behave the same as in Messages app)

https://imgur.com/R2rsqzh

My code for using the searchable is fairly simple:

var body: some View {
        NavigationView {
            List {
                ForEach(/*** movie stuff ***/) { movie in
                    ///// row here
                }
            }
            .listStyle(GroupedListStyle())
            .onAppear {
                // load movies
            }
            .navigationTitle("Discover")
            .searchable(text: $moviesRepository.searchText, placement: .toolbar, prompt: "Search...")
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    So, after adding a progress view above the list view, it suddenly started working the way I want it to. The code now is minimally changed and looks like this.

    var body: some View {
        NavigationView {
            VStack {
                if /** we're doing search, I'm checking search text **/ {
                    ProgressView()
                        .padding()
                }
    
                if !movies.isEmpty {
                    List {
                        ForEach(/** movies list **/) { movie in
                            // movie row here
                        }
                    }
                    .listStyle(GroupedListStyle())
                    .navigationTitle("Discover")
                    .searchable(text: $moviesRepository.searchText, placement: .toolbar,
                                prompt: Text("Search...")
                                    .foregroundColor(.secondary)
                    )
                } else {
                    ProgressView()
                        .navigationTitle("Discover")
                }
            }
        }
        .onAppear {
            // load movies
        }
    }
    

    And basically after adding the progress views, it started working the way I described it in my OP and the way it worked for ChrisR


  2. You can set the displayMode to ".automatic" and it will hidden by default.
    SearchFieldPlacement.NavigationBarDrawerDisplayMode

    List {
    ...
    }
    .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .automatic)) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search