我正在使用tabview并且它工作正常但是如果我通过单击任何地方添加用于解除键盘的代码,tabbar停止工作但所有其他按钮都工作。这是我用来解除键盘的代码:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
答案 0 :(得分:0)
我不确定这是处理此问题的最佳方法,但如果没有经验,这似乎是我所知道的唯一万无一失的方法。这将创建一个不可见的视图,用于识别单击并在单击时删除自身。
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
}
}
答案 1 :(得分:0)
我修复了它,在背景上放了一个按钮并为该按钮调用view.endEditing(true)
它不是最漂亮的修复,但它可以工作