Skip to content

Instantly share code, notes, and snippets.

@ssebi
Last active June 7, 2019 07:40
Show Gist options
  • Save ssebi/ed75c591c1dd216d416ed65f6402f7ff to your computer and use it in GitHub Desktop.
Save ssebi/ed75c591c1dd216d416ed65f6402f7ff to your computer and use it in GitHub Desktop.
Extension to hide the keyboard when tapping around
//
// ViewController.swift
// MasjidLink
//
// Created by Sebastian Vidrea on 08/04/2019.
// Copyright © 2019 Sebastian Vidrea. All rights reserved.
//
import UIKit
extension UIViewController: UIGestureRecognizerDelegate {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
tap.delegate = self
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view is UIButton {
return false
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment