I'm able to automatically focus this TextField when the sheet appears using @FocusState and onAppear, however, I'm finding that the sheet needs to fully appear before the TextField is focused and the on screen keyboard Появится.
Код: Выделить всё
struct ContentView: View {
@State var showSearch = false
var body: some View {
Button {
showSearch = true
} label: {
Text("Search")
}
.sheet(isPresented: $showSearch) {
SearchView()
}
}
}
struct SearchView: View {
@State var searchTerm = ""
@FocusState private var searchFocus: Bool
var body: some View {
TextField("Search", text: $searchTerm)
.focused($searchFocus)
.onAppear() {
searchFocus = true
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/752 ... -appearing
Мобильная версия