Search code examples
swiftuiswiftui-form

How to hide the keyboard when using SwiftUI?


Well I am new in SwiftUI I found a code, just I need put that after the curly bracket in //NavigationView then dismiss the keyboard when I start scrolling., is good But I would like activate that when I choose the percentage in my app. this is my code: Thank you for help me.

Well I am new in SwiftUI I found a code, just I need put that after the curly bracket in //NavigationView then dismiss the keyboard when I start scrolling., is good But I would like activate that when I choose the percentage in my app. this is my code: Thank you for help me.


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("\($0) 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[$0])%")
                }//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()
    }
}

Solution

  • A quick workaround for you:

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

    Then pass customBinding to Picker as in selection: customBinding.

    There are more complex solutions to this problem and I would only consider this as a quick workaround.