0

I try to use the new .searchable feature. The following code works as expected:

import SwiftUI

struct ContentView: View {

   @State private var searchText = ""

   var body: some View {
        NavigationView {
        List {
            ForEach(0..<100) { index in
                Text("Row \(index)")
            }
        }
        .searchable(text: $searchText)
        .navigationTitle("Title")
//        .navigationBarHidden(true)
       }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
       ContentView()
    }
 }

If you remove the comment, the searchbar is no longer available. Any idea why this happens and how to avoid this?

madcat
  • 322
  • 3
  • 14
  • 2
    Because the placement of the search field for `.searchable() is in the .navigationBarDrawer`, `.sidebar` or `.toolbar`, all of which are hidden with `.navigationBarHidden(true)`. – Yrb Feb 02 '22 at 20:42
  • Do you know if there is a workaround? – madcat Feb 04 '22 at 18:42
  • Roll your own search bar. There are tons of examples. If you want `.searchable()` you have to have a navigation bar. What is the issue with the navigation bar? – Yrb Feb 04 '22 at 18:56
  • I have a static element on top off the screen. (see here: https://www.icloud.com/iclouddrive/086sZp6woQHvkZ2j5kz7HPWLA#Simulator_Screen_Shot_-_iPhone_13_mini_-_2021-11-19_at_17.27.56). The list below has exact the right behaviour I desired. I have solved the problem by adding a search button. Thanks for your support. – madcat Feb 05 '22 at 17:11

0 Answers0