用tabbar解析键盘
问题描述:
我使用的是tabview,它工作正常,但如果我通过点击任何地方tabbar停止工作,但所有其他按钮的工作,添加解除键盘的代码。这里是我用来消除键盘代码:用tabbar解析键盘
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
答
我不知道这是处理这一目标的最佳方式,但没有experiementing,这似乎是唯一的万无一失的方法,我了解。这将创建一个不可见的视图,以识别点击并在点击后自行删除。
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)
它是不是最漂亮的修复作用,但它的工作原理