我想让搜索栏在视图出现后立即成为第一响应者。不幸的是,目前还没有@FocusState支持/其他调用来实现这个开箱即用的功能。还有其他方法可以实现这一目标吗?
struct SearchDemo: View {
@State var searchString : String = ""
var body: some View {
NavigationView {
List{
Text("...")
}
.searchable(text: $searchString)
}
}
}
Run Code Online (Sandbox Code Playgroud)
A bit hacky, but you can get it working with Introspect. It's not quite "soon as the view appears," but it's the best I've been able to figure out without rolling my own search controller.
struct SearchView: View {
@State var searchText: String = ""
var body: some View {
NavigationStack {
SomeList()
.searchable(text: $searchText)
}
.introspectSearchController(customize: { controller in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
controller.searchBar.searchTextField.becomeFirstResponder()
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |