엑스코드(Xcode)/Swift 4

[Swift 4] 키보드 내리기 (hideKeyboard) - dismissKeyboard

Clein8 2018. 12. 2. 18:14

TextField 이외에 부분에 Tap하면, 키보드 내리기.


ViewContoller.swift (Swift 4)

class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.hideKeyboard() // <- 추가 } } // extension 추가 extension UIViewController { func hideKeyboard() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) view.addGestureRecognizer(tap) } @objc func dismissKeyboard() { view.endEditing(true) } }