HideKeyBoard when tap outside of TextField or scroll a scrollView in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* For iOS 7 or later | |
* 1. In IB, set keyboard attr. to "Dismiss on drag" | |
* or in ViewDidLoad write | |
* self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag | |
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/c/tdef/UIScrollViewKeyboardDismissMode | |
* 2. Add UITapGestureRecognizer in ViewDidLoad | |
*/ | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
// Your init code here | |
// This only catch tapGesture outside of textField. keyboardDismissMode has to be set for scrolling(not tap) scrollView | |
var tapGesture = UITapGestureRecognizer(target:self, action: "hideKeyBoard") | |
tapGesture.cancelsTouchesInView = false | |
self.view.addGestureRecognizer(tapGesture) | |
} | |
func hideKeyBoard() { | |
self.myTextField.resignFirstResponder() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment