UIButton Click Event not working if I add UITapGestureRecognizer in swift

♀尐吖头ヾ 提交于 2020-07-14 04:15:07

问题


On my login page, if I focus to UITextEdit for entering email and password, keyboard appears and login page scroll up. And I add this code for remove keyboard if I touch outside of keyboard.

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    
    @objc func dismissKeyboard() {
        view.endEditing(true)
        UIView.animate(withDuration: 0.3, animations: {
            self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        })
    }
}

And on my viewDidLoad() function, I added it like this.

override func viewDidLoad() 
{
        super.viewDidLoad()
        //TODO
        self.hideKeyboardWhenTappedAround()
       ...
}

It works well, But If I click login button when keyboard is still opening, dismisskeyboard() function works but btnClick function doesn't work.

@IBAction func LoginBtnClick(_ sender: Any) 
{
        print("loginBtn")
        //...
}

How can I solve this problem?


回答1:


Don't add the gesture to main viewController's view add a full screen subview below UIButton and add the gesture to it




回答2:


I have tested it using following code:

import UIKit

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
        UIView.animate(withDuration: 0.3, animations: {
            self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        })
    }
}

class ViewController: UIViewController {

    fileprivate let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 400, height: 50))
    fileprivate let button = UIButton(frame: CGRect(x: 20, y: 200, width: 150, height: 50))

    override func loadView() {
        super.loadView()

        view.addSubview(textField)
        textField.backgroundColor = .lightGray
        view.addSubview(button)
        button.setTitle("Press me", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.addTarget(self, action: #selector(a), for: .touchUpInside)

        hideKeyboardWhenTappedAround()
    }

    @objc func a() {
        print(">>>> pressed")
    }
}

The code in a gets called. Thus I assume that there is something else in your code that causes the button not to work. Perhaps you forgot to link the button to the @IBAction LoginBtnClick(_:) - check if the connection is really there.




回答3:


I actually solved it a little different way (I had the same problem on my login screen). I just moved the dismissing/animating of the keyboard into an async block with a tiny delay, because it seemed like with the tap recognizer, it was picking up the touch down, but not the touch up inside the button, likely because the screen had already started to animate the dismissal of the keyboard.

@objc func dismissKeyboard() {
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
        self.view.endEditing(true)
        UIView.animate(withDuration: 0.3, animations: {
            self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        })
    }
}



回答4:


I added a button over the view of view controller (be sure that your textfield is in front of the button). Then, I implemented the UITextFieldDelegate to enable/disable the button. Something like this:

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet private weak var myTextfield: UITextField!
    @IBOutlet private weak var myButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.myTextfield.delegate = self
    }
    
    // MARK: - UITextFieldDelegate
    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.myButton.isUserInteractionEnabled = true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.myButton.isUserInteractionEnabled = false
    }
    
    // MARK: - Action functions.
    @IBAction private func onButtonAction() {
        self.myTextfield.resignFirstResponder()
    }
}


来源:https://stackoverflow.com/questions/48211042/uibutton-click-event-not-working-if-i-add-uitapgesturerecognizer-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!