9

I'm using iOS 15 and trying out the new searchable modifier on Lists in SwiftUI. It looks like when you attach searchable(text: $searchText) to a NavigationView the search bar renders on the screen by default below the navigation bar title. When I've tried using it and I run the simulator, the search bar isn't rendered on the screen. Only when I start scrolling down on the screen through the list, does the search bar appear on the screen.

Is this expected behavior?

Here's the code.

@State private var searchText = ""
let data = (1...50).map( {_ in Int.random(in: 1...10)} )

var body: some View {
    NavigationView {
        List {
            ForEach(data, id: \.self) { d in
                Text("\(d)")
            }
        }
    }.searchable(text: $searchText)
}

Before scrolling

enter image description here

After scrolling

enter image description here

Petesta
  • 1,623
  • 3
  • 19
  • 29

1 Answers1

14

It is default behavior, to see it always we need to provide corresponding placement, like

NavigationView {
    List {
        ForEach(data, id: \.self) { d in
            Text("\(d)")
        }
    }
}
.searchable(text: $searchText, 
       placement: .navigationBarDrawer(displayMode: .always))  // << here !!
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • This is not the default behaviour - the default behaviour is for the search bar to appear under the title and disappear when you scroll. Given the date of this question, its likely that this was a beta bug as it does not happen in iOS 15 final. – Luke Redpath Jan 13 '22 at 11:11
  • It does happen in iOS 15.6.1 There is no way to have the search field shown initially and hide as the list scrolls. It's either unobvious or obnoxious behaviour of the search field, nothing in the middle. – Anton Tropashko Nov 25 '22 at 15:22
  • On iPad running 16.1 search field does show initially – Anton Tropashko Nov 25 '22 at 15:24