Ios 使用tabbar在视图中显示缺少的键盘

Ios 使用tabbar在视图中显示缺少的键盘,ios,swift,uitabbar,Ios,Swift,Uitabbar,我使用的是tabview,它工作得很好,但是如果我添加了关闭键盘的代码,单击任何地方,tabbar都会停止工作,但所有其他按钮都会工作。以下是我用来关闭键盘的代码: extension UIViewController { func hideKeyboardWhenTappedAround() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewC

我使用的是tabview,它工作得很好,但是如果我添加了关闭键盘的代码,单击任何地方,tabbar都会停止工作,但所有其他按钮都会工作。以下是我用来关闭键盘的代码:

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

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

}

我不确定这是处理这个问题的最佳方法,但如果不进行实验,这似乎是我所知道的唯一简单易行的方法。这将创建一个不可见视图,该视图可以识别单击并在单击时删除自身

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
    }
}

我不确定这是处理这个问题的最好方法,但如果没有实验,这似乎是我所知道的唯一万无一失的方法。这将创建一个不可见视图,该视图可以识别单击并在单击时删除自身

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
    }
}

我修复了在背景上放置一个按钮并为按钮调用
view.endEditing(true)进行操作的问题

这不是最漂亮的修复方法,但效果很好

我修复了它,在后台放置了一个按钮,并为该按钮调用了操作
视图。endEditing(true)
这不是最漂亮的修复方法,但效果很好