UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: how to call function when keyboard disappears

Forums > SwiftUI

Another beginner's question! I use this code, which I picked up from someone helpful (perhaps here), so that a tap anywhere dismisses the keyboard being shown:

extension UIApplication {
    func addTapGestureRecognizer() {
        guard let window = windows.first else { return }
        let tapGesture = UITapGestureRecognizer(target: window, action: #selector(UIView.endEditing))
        tapGesture.cancelsTouchesInView = false
        tapGesture.delegate = self
        tapGesture.name = "MyTapGesture"
        window.addGestureRecognizer(tapGesture)
    }
}

It works very nicely. I'd like to be able to call a function when the keyboard is dismissed, but my knowledge of iOS outside SwiftUI is pretty much zero. Can I add a function call to this code somehow?

3      

You have a UITapGestureRecognizer which has an action associated with it. This gets called when there is a tap. You can specify a custom function to run here.

Here is an example of what you can add:

@objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
  // do what you need to do here
}

You will need to change the action parameter to #selector(dismissKeyboard).

3      

Thanks - I'd tried changing the #selector to a function, but no matter how I define the function, I get a crash immediately:

    let tapGesture = UITapGestureRecognizer(target: window, action: #selector(dismissKeyboard))

-[UIWindow dismissKeyboard:]: unrecognized selector sent to instance 0x7fd716909110

I've tried various declarations of dismissKeyboard, but the result is always the same.

Jeremy

3      

What your error message is saying is that the UIWindow object has no function called dismissKeyboard. The target of the tap gesture is the first window in the application, but you have added the dismissKeyboard function to an instance of UIApplication. There is nothing wrong with dismissKeyboard itself: it's just how you have defined your UITapGestureRecognizer.

To fix this, make sure the target you pass into the tap gesture has the dismissKeyboard function. There are two ways that I would go about this:

  1. Either change the target to be self
  2. Or add the dismissKeyboard function to the instance of UIWindow

3      

This reminds me of learning how to write for MacOS in the 1980s, when it was said that in order to understand any one of the five volumes of Inside Mac, one had first to have read and understood the other four.

I'm afraid I'm going to parade my ignorance again: how do I do what you suggest?

At present, with the code as it stands, @objc func hideKeyboard() lives inside extension UIApplication, and my @main function (largely supplied by XCode) reads

@main
struct fooApp: App {
    var body: some Scene {
        WindowGroup {
            Calculator()
                .onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
        }
    }
}

and with #selector(UIView.endEditing) it works fine (Calculator() is a NavigationView with a variety of destinations). When I change it to #selector(dismissKeyboard), the crash happens as soon as I tap on a link in the NavigationView, presumably because a new window is opened which doesn't have dismissKeyboard() attached. How do I accomplish either attaching to self or adding it to each new window?

3      

Maybe this will help:

extension UIApplication: UIGestureRecognizerDelegate {
    func addTapGestureRecognizer() {
        guard let window = windows.first else { return }
        let tapGesture = UITapGestureRecognizer(target: window, action: #selector(UIWindow.dismissKeyboard))
        tapGesture.cancelsTouchesInView = false
        tapGesture.delegate = self // requires conformance to UIGestureRecognizerDelegate
        tapGesture.name = "MyTapGesture"
        window.addGestureRecognizer(tapGesture)
    }
}

extension UIWindow {
    @objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
        print("Tapped")
    }
}

I've tested this and it seems to work. Every time I tap on the screen, the "Tapped" message is printed out. I'm guessing this is what you want.

3      

It does work, and it is exactly what I was looking for. Thanks very much. I've learned something as well as having a problem fixed.

Jeremy

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.