使用SwiftUI时如何隐藏键盘?

How to hide the keyboard when using SwiftUI?

好吧,我是 SwiftUI 的新手,我找到了一个代码,我只需要将它放在 //NavigationView 中的大括号之后,然后在我开始滚动时关闭键盘。很好,但是我想在我选择时激活它我的应用程序中的百分比。这是我的代码: 谢谢你的帮助。

好吧,我是 SwiftUI 的新手,我找到了一个代码,我只需要将它放在 //NavigationView 中的大括号之后,然后在我开始滚动时关闭键盘。很好,但是我想在我选择时激活它我的应用程序中的百分比。这是我的代码: 谢谢你的帮助。


struct ContentView: View {

    @State var cantidad = ""
     @State private var numberOfPeople  = ""
    @State private var tippercentage  = 2
    let tipPercentages = [10,15,20,25,0]

    var totalPersonas: Double{
        //Funcion que calcula el total de personas
        //let conteopersonas = Double(numberOfPeople + 2  )
        let conteopersonas = Double(numberOfPeople) ?? 0
        let PropinaSeleccion = Double(tipPercentages[tippercentage])
        let MontoOrden = Double(cantidad) ?? 0

        let propinaValor = MontoOrden / 100 * PropinaSeleccion
        let Totalgeneral = MontoOrden + propinaValor
        let MontoPersonas = Totalgeneral / conteopersonas
        return MontoPersonas
    }

    var metoMontotalcuenta: Double {
        let PropinaSeleccion2 = Double(tipPercentages[tippercentage])
       let MontoOrden2 = Double(cantidad) ?? 0

        let propinavalor2 = MontoOrden2 / 100 * PropinaSeleccion2
        let TotalGeneralCuenta = MontoOrden2 + propinavalor2


        return TotalGeneralCuenta
    }



    var body: some View {

        NavigationView{
        Form {
            Section (header: Text("Monto de la cuenta"))  {
                 TextField("Ingresa Monto de la cuenta", text: $cantidad)
                    .keyboardType(.decimalPad)

              //  Picker("Numero de personas", selection: $numberOfPeople){
              //      ForEach(2 ..< 100) {
              //          Text("\([=10=]) people")
              //      }
                //}

                Section (header: Text("Numero de Personas")){//seccion Personas
                    TextField("Ingresa El numero de personas", text:$numberOfPeople)
                    .keyboardType(.decimalPad)
                           }//seccion Personas

            }//Seccion1
            Section(header: Text("Cuanta propina quieres dejar")) {

            Picker("Tipo de porcentage", selection: $tippercentage){

                ForEach(0 ..< tipPercentages.count){
                    Text("\(self.tipPercentages[[=10=]])%")
                }//ForEach

            }//Picker
            .pickerStyle(SegmentedPickerStyle())


            }//Seccion3
            //Seccion 2
            Section (header: Text("Cantidad por persona")) {

              Text("Cada persona pone:$\(totalPersonas)")
            } //Seccion 2

            Section{//seccion 4
                 Text("Monto total de la cuenta:$\(metoMontotalcuenta)")
            }//seccion 4






        } //Form
        .navigationBarTitle("Propinas SwiftUi")
        } //NavigationView
             .gesture(DragGesture().onChanged{_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)})

    }


}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

适合您的快速解决方法:

    private var customBinding: Binding<Int> {
        Binding(get: {
            self.tippercentage
        }, set: {
            self.tippercentage = [=10=]
            UIApplication.shared.windows.forEach { [=10=].endEditing(true )}
        })
    }

然后像 selection: customBinding 一样将 customBinding 传递给 Picker

这个问题有更复杂的解决方案,我只认为这是一个快速的解决方法。