TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

.searchable and managing focus

Forums > SwiftUI

@cymr0  

Hi, does anybody know how to make the.searchable textfield have focus? It's a search view, so it makes sense to automatically make it have focus when it loads, but .focusState doesn't seem to work with it? Any ideas would be appreciated!

struct CustomerSearchView: View {
    @Binding var customer: Customer?

    @State private var searchName = ""
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var customerSearchViewModel: CustomerSearchViewModel
    @FocusState private var searchFocused: Bool

    var body: some View {
        NavigationStack {
            List {
                if customerSearchViewModel.shouldShowNoResults {
                    Text("🤔 No Results")
                } else if customerSearchViewModel.shouldShowProgressView {
                    ProgressView()
                }

                ForEach(customerSearchViewModel.customers) { searchResult in
                    SearchResultItemView(searchResult: searchResult)
                        .onTapGesture {
                            self.customer = searchResult
                            presentationMode.wrappedValue.dismiss()
                        }
                }
            }
            .searchable(text: $customerSearchViewModel.searchText, prompt: "Search by name, email or postcode")
            .focused($searchFocused)
            .autocorrectionDisabled(true)
            .keyboardType(.alphabet)                // stop keyboard suggestions
            .textContentType(.init(rawValue: ""))   // stop keyboard suggestions
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
                    searchFocused = true
                }
            }
            .onDisappear {
                customerSearchViewModel.searchText = ""
                customerSearchViewModel.customers.removeAll()
            }
        }
        .alert(isPresented: $customerSearchViewModel.hasError, error: customerSearchViewModel.error) { }
    }
}

3      

I'm assuming that I'm missing some nuance since I'm still pretty new to this, but I came accross your question when I had the same challenge. I found an answer in Apple's docs that is working well for me:

struct SheetView: View {
    @State private var isPresented = true
    @State private var text = ""

    var body: some View {
        NavigationStack {
            SheetContent()
                .searchable(text: $text, isPresented: $isPresented)
        }
    }
}  

I realize it is way to late to solve your problem but perhaps anyone else coming across this might benefit.

4      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.