gpt4 book ai didi

ios - 使用标签栏在 View 中关闭键盘

转载 作者:行者123 更新时间:2023-11-28 08:29:38 26 4
gpt4 key购买 nike

我正在使用 tabview,它工作正常,但如果我添加代码以通过单击任何地方来关闭键盘,则 tabbar 停止工作,但所有其他按钮工作。这是我用来关闭键盘的代码:

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

func dismissKeyboard() {
view.endEditing(true)
}

最佳答案

我不确定这是处理此问题的最佳方法,但未经实验,这似乎是我所知道的唯一万无一失的方法。这将创建一个不可见的 View ,它可以识别点击并在点击时自行删除。

class ThisViewController: UITableViewDelegate, UITableViewDatasource {

var tapView: ClickThroughView?

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
tapView = ClickThroughView(frame: view.frame)
tapView?.delegate = self
view.addSubview(tapView!)
view.bringSubviewToFront(tapView!)
}

func keyboardWillHide() {
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
}

extension ThisViewController: HandleViewTapDelegate {
func handleTap() {

//code on tap not on keyboard
view.endEditing(true)

tapView?.removeFromSuperview()
tapView = nil
}
}

protocol HandleViewTapDelegate {
func handleTap()
}

class ClickThroughView: UIView {
var delegate: HandleViewTapDelegate?

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
delegate?.handleTap()
return false
}
}

关于ios - 使用标签栏在 View 中关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com