SwiftUI .searchable in iOS 15 - .navigationBarDrawer(displayMode: .always)

Viewed 1261

I want to achieve a Search Bar with the new .searchable modifier, with .navigationBarDrawer(displayMode: .always). But if I start the App, the default position of the Search Bar is already "scrolled".

When I open the App

But this is what I want to achieve at the startup of the App:

What I want to achieve

This is my code:

struct SearchView: View {
    @State var searchText = ""
    var body: some View {
        NavigationView {
            List {
                Text("Search Bar")
            }
            .navigationBarTitle("Search")
            .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
        }
    }
}
1 Answers

Hacky Solution but it worked:

struct ContentView: View {
@State var searchText = ""
var body: some View {
    NavigationView {
        ZStack {
            Rectangle()
                .fill(Color.white)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 1)
            
            List {
                Text("Search Bar")
            }
        }
        .searchable(text: $searchText, placement:.navigationBarDrawer(displayMode: .always))
        
        .navigationBarTitle("Search", displayMode: .large)
    }
}

}

Related