2016-08-22 4 views
1

タブビューを使用していますが、タブバーを使用してキーボードを閉じるコードを追加すると、タブバーは機能しなくなりますが、他のボタンはすべて機能します。ここで私は、キーボードを消すために使用されるコードは次のとおりです。タブバで表示されているキーボードを無効にする

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

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

}

答えて

0

私は、これはこれを処理するための最良の方法であることを確認していないが、experiementingせずに、これが唯一の確実な方法Iのようです知ってる。これにより、クリックを認識し、クリック時に自分自身を削除する非表示のビューが作成されます。

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

私はそれはきれいな修正ではないが、それは

を働くことが背景にあるボタンを置くと、そのボタンのコール view.endEditing(true) のためのアクションを作っ固定
関連する問題