Advertisement
neatekFb

Hide keyboard on touch textfield if keyboard opened // swift

Nov 22nd, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.89 KB | None | 0 0
  1. //// Vladimir Zhelnov - neatek.pw - Web/iOS dev
  2. var keyboard_s = false
  3.  
  4. override func viewDidLoad() {
  5.         NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  6.        
  7.         NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
  8.        
  9.         HERE_IS_TEXTFIELD.addTarget(self, action: #selector(ViewController.myTargetFunction), for: UIControlEvents.touchDown)
  10. }
  11.  
  12. func myTargetFunction(textField: UITextField) {
  13.     if(keyboard_s) {
  14.         view.endEditing(true)
  15.     }
  16. }
  17.    
  18. func keyboardWillShow(notification:NSNotification) {
  19.     print("Show")
  20.     keyboard_s = true
  21. }
  22.    
  23. func keyboardWillHide(notification:NSNotification) {
  24.     print("Hide")
  25.     keyboard_s = false
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement