TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Unable to add Done button to keyboard to dismiss keyboard after finishing typing in TextField

Forums > SwiftUI

I've been wracking my brain trying to get this to work for a day now and tried numerous methodologies from HWS forums, SO, and other YT vids and none work for me! I cannot even get to the point of testing the dismiss keyboard code as I cannot even get the Done button to successfully display on the keypad! Someone please save me, I'm at a loss.

   @FocusState private var fieldIsFocused: Bool

   let freqs = ["Monthly", "Quarterly", "Semi-Annually", "Annually"]

   var body: some View {
      NavigationView {
         ZStack {
            Form {
               VStack {
                   HStack(alignment: .center) {
                     Text("Symbol:")
                        .font(.callout)
                        .bold()
                     TextField("Symbol", text: $symbol)
                        .focused($fieldIsFocused)
                        .submitLabel(.done)
                     Button("Done") {
                        self.hideKeyboard()
                     }
                        .keyboardType(.alphabet)
                        .disableAutocorrection(true)

                  }
               }

               HStack(alignment: .center) {
                  Text("Dividend Amount:")
                     .font(.callout)
                     .bold()
                  TextField("Dividend Amount", value: $dividend_amt, format: .number)
                     .textFieldStyle(RoundedBorderTextFieldStyle())
                     .keyboardType(.decimalPad)
                     .focused($fieldIsFocused)
               }

            } //END FORM: NEW STACK TO ISOLATE PICKER

            Picker("Frequency", selection: $frequency_sel) {
               ForEach(freqs, id: \.self) {
                  Text("\($0)")
               }
            }

         } //END ZSTACK
         .toolbar {

            ToolbarItemGroup(placement: .keyboard) {
               Spacer()
               Button("Done") {
                  fieldIsFocused = false
               }
            }

            ToolbarItem(placement: .bottomBar) {
               HStack {
                  Button {
                     let investment = Investments(context: moc)
                        ....
                     try? moc.save()
                     dismiss()
               } label: {
                  Text("Add")
                  Image(systemName:"cross.circle.fill")
               }
               Button {
                  dismiss()
               } label: {
                  Text("Cancel")
                  Image(systemName:"xmark.app.fill")
               }
            } //END: HSTACK

             } //END: TOOLBARITEM(bottombar)

         }//END: TOOLBAR
      } //END: NAVIGATIONVIEW
         } //END: BODY

2      

Hi @a_sneaky_pete ! What do you mean by displaying Done button on keypad? Do you mean adding a button on virtual keyboard dispalyed by iOS itself?

2      

@ygeras, yes exactly.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

As far as I know, there is no API provided by iOS to do that for number pads. Only for "full" keyboard type you can change it similar to .submitLabel(.done) . But you have "Done" button added to your code.

ToolbarItemGroup(placement: .keyboard) {
               Spacer()
               Button("Done") {
                  fieldIsFocused = false
               }
            }

it can also serve the same purpose of dismissing, saving, etc... Or what I am missing here?

2      

Maybe try this for iPhone:

                        Image(systemName: "keyboard.chevron.compact.down")
                            .onTapGesture {
                                hideKeyboard()
                            }
                            .foregroundColor(Color.blue)
                            .buttonStyle(.borderless)

2      

@swsharpy, should I be putting that code inside of the ToolbaritemGroup struct?

2      

See below code for how I used the hideKeyboard(). Not certain where it would go in your code.

                    TextField("Enter weight", value: $weight, format: .number)
                        .frame(width: 50, height: 5)
                        .font(Font.system(size: 16))
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 10) .fill(Color(UIColor.white)))
                        .border(.black)
                        .foregroundColor(Color.black)
                    Spacer()

                    if (fnDevice() == "iPhone") {
                        Image(systemName: "keyboard.chevron.compact.down")
                            .onTapGesture {
                                hideKeyboard()
                            }
                            .foregroundColor(Color.blue)
                            .buttonStyle(.borderless)
                    }

2      

@ygeras, the Done button code is not working as expected when I run my app (i.e. there is no Done Button being displayed on the keyboard.

2      

You want it here as in pic, right? https://ibb.co/7RfkM3X

2      

@ygeras, yes, your pic is a good example of what I'm looking for.

2      

@swsharpy, where are you getting fnDevice() from?

2      

Well basically this code is responsible for that.

 ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        fieldIsFocused = false
                    }
                }

and it works perfectly when I copy your code... try to clean up build folder maybe... cmd + shift + K

2      

Sorry, fnDevice() just checks if iPad or iPhone.

// Function to return current device
func fnDevice() -> String {
    if UIDevice.current.localizedModel == "iPhone" {
        return "iPhone"
    } else if UIDevice.current.localizedModel == "iPad" {
        return "iPad"
    }
    return "nil"
}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.