//適当な場所をタッチでキーボードを閉じる
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
//リターンキーを押した時にキーボードを閉じる
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//キーボードが閉じる
textField.resignFirstResponder()
return true
}
これを書くだけでOK。
(viewDidLoad内に書かなくてよい。)