【发布时间】:2020-04-15 20:25:31
【问题描述】:
我正在使用SwiftUI 创建一个Form。在我的Form 中,我有一个DatePicker、TextField 和一个SegmentedPickerStyle。
我的TextField 正在使用.decimalPad,我正在尝试找到在完成输入后关闭键盘的最佳方式。
我尝试添加.onTapGesture,但它阻止我使用任何我的选择器。当我点击它们时,没有任何反应。
这是我想要做的:
struct ContentView: View {
@State var date = Date()
@State var price = ""
@State private var tipPercentage = 2
let tipPercentages = [10, 15, 20, 25, 0]
var body: some View {
NavigationView {
Form {
Section {
DatePicker(selection: $date, displayedComponents: .date) {
Text("Date").bold().foregroundColor(Color.init(red: 100.0/255.0, green: 208.0/255.0, blue: 100.0/255.0))
}
HStack {
Text("Price"))
Spacer()
TextField("required", text: $price).multilineTextAlignment(.trailing).keyboardType(.decimalPad)
}
}
Section(header: Text("How much tip do you want to leave?")) {
Picker("Tip percentage", selection: $tipPercentage) {
ForEach(0 ..< tipPercentages.count) {
Text("\(self.tipPercentages[$0])%")
}
}
.pickerStyle(SegmentedPickerStyle())
}
}
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow!.endEditing(true)
}
}
}
}
【问题讨论】:
-
由于 iOS 15 中仍然存在此错误,因此我提交了雷达:openradar.appspot.com/radar?id=5041898704076800
标签: ios swiftui gesture picker