HomeiOS Developmentios - TextField not auto-focusing onAppear?

ios – TextField not auto-focusing onAppear?


I am making an attempt to make my TextField auto-focus onAppear, when the web page reveals.

Right here is my code:

import SwiftUI
struct SearchView: View {
    @FocusState var isFocused: Bool
    
    @State personal var searchText = ""
    @State personal var isEditing = false
    
    var physique: some View {
        NavigationStack {
            VStack {
                ZStack(alignment: .heart) {
                    TextField(
                        "Seek for a spot",
                        textual content: $searchText
                    )
                    .centered($isFocused)
                    .onTapGesture {
                        self.isEditing = true
                    }
                    .autocorrectionDisabled()
                    .textInputAutocapitalization(.phrases)
                    .submitLabel(.search)
                    .onAppear {
                        DispatchQueue.important.asyncAfter(deadline: .now() + 0.1) {
                            isFocused = true
                        }
                    }
                }
            }
        }
    }
}

However the TextField does not auto-focus when the web page reveals (i.e. the keyboard does not seem and I am unable to instantly begin typing).

I’ve additionally tried:

.onAppear {
    isFocused = true
}

In addition to:

.activity {
    isFocused = true
}

In addition to:

.activity {
    // Process runs after view is totally in hierarchy
    attempt? await Process.sleep(nanoseconds: 300_000_000) // 0.3s
    isFocused = true
}

However none of those are working. Why?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments