Я создаю Form
, используя SwiftUI
. В моем 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)
}
}
}
}