如果我在swift中添加UITapGestureRecognizer,则UIButton单击事件将不起作用 [英] UIButton Click Event not working if I add UITapGestureRecognizer in swift
问题描述
在我的登录页面上,如果我专注于UITextEdit来输入电子邮件和密码,则会出现键盘,并且登录页面会向上滚动.而且,如果我触摸键盘以外的地方,则会添加此代码以删除键盘.
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)
})
}
}
然后在我的viewDidLoad()函数上,像这样添加它.
And on my viewDidLoad() function, I added it like this.
override func viewDidLoad()
{
super.viewDidLoad()
//TODO
self.hideKeyboardWhenTappedAround()
...
}
它工作正常,但是如果在键盘仍处于打开状态时单击登录按钮,则dismisskeyboard()函数有效,但btnClick函数无效.
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?
推荐答案
不要将手势添加到主viewController的视图中,在UIButton下方添加全屏子视图并将手势添加到其中
Don't add the gesture to main viewController's view add a full screen subview below UIButton and add the gesture to it
这篇关于如果我在swift中添加UITapGestureRecognizer,则UIButton单击事件将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!