SwiftUI Dismiss Keyboard

Hi,

First off… thank you for the help !

SwiftUI… I have a simple text field and when the user taps on it, I have managed to have a Decimal Keyboard appear.

However I need it to be able to dismiss as well, perhaps by simply taping on another part of the screen.

I have searched and searched, but have found nothing simple to dismiss the keyboard.

Hoping you have a simple solution.

Thanks.

Craig.

Create a button and have an IBOutlet for it. When the user taps the button, it should take the input from the text field and store it in a string, like this:
@IBAction func GuessButton(_ sender: AnyPreformatted text) {

//Takes input of type here box

inp = TypeHereBox.text!

Let me know if this works
YourAvgCoder

1 Like

Hi,
Thanks for the eccellent reply…
However I am just running SwiftUI so there is not IBOutlet… I’m a newbie, perhaps I’m missing something.

Cheers.

Craig.

Hi Craig,

This should get you going anyway. I figured out how to get the tapGesture in the TextField to dismiss the keyboard just not the empty space yet. I am out of time, but this should get you going ion the right direction anyway.

extension View {
        func endEditing(_ force: Bool) {
            UIApplication.shared.windows.forEach { $0.endEditing(force)}
        }

And then to the Container, VStack, HStack, add the following modifier to call it.

         .onTapGesture {
            self.endEditing(true)
            
         }

When I get a chance, I will see if I can experiment with adding it to a Spacer() or something.

If you figure it out before I get to it, I would love to hear how you solved this.

I am REALLY new to SwiftUI so I am bumbling my way through it.

If you were asking about the “old way” I would have an answer in a shot! lol

Good luck.
Blessings,
—Mark

1 Like

Hi Mark,

Great mate… I’ll see how I go with it… I am and old newbie and find it hard to learn but continue on.

Thanks for the idea… will give it a go and let you know.

Yes… UIKit would have been a breeze.

Cheers.

Craig

Well Craig, I need a smarter person to chime in.

I can get this simple call to work with any upbeat in a HStack or VStack but how to capture the tap outside of the objects in the view have alluded me.

I am sure it can be done, I just have not the grey matter to figure it out! lol

Stand by…
Blessings,
—Mark

1 Like

Hi Mark,

Me neither mate… I have added a Button beside the Textfield which when tapping on the button and if the keyboard is showing… will Dismiss the keyboard…

It’s a bit clunky, so I will keep at it… I’ll let you know how I go.

I love the layout features of SwiftUI, but miss UIKit as well.

I do this as a hobby and keep the old brain clicking over, and meeting great people like yourself.

Cheers.

Craig.!

Hey Mark,

Hope your Christmas was great… the new year is next.

Hey further to this… As I managed to create a button to dismiss the keyboard when the button is tapped, how about if a button was the entire size of the screen but behind other controls, therefor when someone tapped on a blank piece of the screen, it would then tap the button and dismiss the keyboard?

Just not sure how to make a button behind other controls and also make it the entire size of the screen.

Any thoughts on that ?

Cheers.

Craig.

Hi Craig,

Day 5 of the Christmas feast it going wonderfully, thanks!

I am JUST now exploring SwiftUI, so I am not sure as I have not spent much time with it as yet.

In terms of layer order, I believe the ZStack will provide layer controls. If I get a chance, I will do some experimentation.

Blessings,
—Mark

1 Like