如果我在 swift 中添加 UITapGestureRecognizer,则 UIButton 单击事件不起作用

UIButton Click Event not working if I add UITapGestureRecognizer in swift(如果我在 swift 中添加 UITapGestureRecognizer,则 UIButton 单击事件不起作用)
本文介绍了如果我在 swift 中添加 UITapGestureRecognizer,则 UIButton 单击事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的登录页面上,如果我专注于 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 单击事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)