Last active
June 7, 2019 07:40
-
-
Save ssebi/ed75c591c1dd216d416ed65f6402f7ff to your computer and use it in GitHub Desktop.
Extension to hide the keyboard when tapping around
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
// | |
// 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