Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeta611/5ca491af89ed912b54ae1836716ad57b to your computer and use it in GitHub Desktop.
Save Zeta611/5ca491af89ed912b54ae1836716ad57b to your computer and use it in GitHub Desktop.
[UIViewController+hideKeyboardWhenTappedAround] #extension #iOS
//
// UIViewController+hideKeyboardWhenTappedAround.swift
//
// Created by Jay Lee on 18/02/2019.
// Copyright © 2019 Jay Lee <jaeho.lee@snu.ac.kr>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
//
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc private func dismissKeyboard(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment