我在视图中进行了搜索TextField,该搜索被触发以显示在ContentView.
当使用和TextField出现工作表时,我可以自动聚焦它,但是,我发现工作表需要在聚焦和屏幕键盘出现之前完全显示。@FocusStateonAppearTextField
这感觉非常慢,我注意到在许多其他应用程序中它们能够触发屏幕键盘和同时出现的工作表。
这是我的代码:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有不同的方法可以使键盘随着工作表的出现而出现,从而使整体体验感觉更加无缝?
这是一种使用自定义工作表的方法,可以更早地引入键盘。但不确定是否值得付出努力:
struct ContentView: View {
@State var showSearch = false
var body: some View {
ZStack {
Button {
withAnimation {
showSearch = true
}
} label: {
Text("Search")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
if showSearch {
SearchView(isPresented: $showSearch)
.transition(.move(edge: .bottom))
}
}
// .sheet(isPresented: $showSearch) {
// SearchView()
// }
}
}
struct SearchView: View {
@Binding var isPresented: Bool
@State var searchTerm = ""
@FocusState private var searchFocus: Bool
var body: some View {
Form {
TextField("Search", text: $searchTerm)
.focused($searchFocus)
Button("Close") {
searchFocus = false
withAnimation {
isPresented = false
}
}
}
.onAppear() {
searchFocus = true
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1118 次 |
| 最近记录: |