我想在敲出文本框时关闭键盘,以便为该文本框下方的选择器留出空间。
struct ContentView: View {
@State private var date = Date()
@State private var text = "write something...."
var body: some View {
Form {
Section {
TextField(text, text: $text)
}
Section {
DatePicker(selection: $date, displayedComponents: .date, label: { Text("select a date")})
.onTapGesture {
self.dismissKeyboard()
}
}
}
}
func dismissKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
问题是.onTapGesture
确实关闭了键盘,但没有显示拾纸轮。那么,在关闭键盘后是否有办法调用隐藏的滚轮?
答案 0 :(得分:0)
使用.onAppear
代替.onTapGesture
DatePicker(selection: $date, displayedComponents: .date, label: { Text("select a date")})
.onAppear {
self.dismissKeyboard()
}