Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
HideKeyBoard when tap outside of TextField or scroll a scrollView in Swift
* For iOS 7 or later
* 1. In IB, set keyboard attr. to "Dismiss on drag"
* or in ViewDidLoad write
* self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
* 2. Add UITapGestureRecognizer in ViewDidLoad
override func 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
func hideKeyBoard() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment