改行ボタン押忍でtextFieldのキーボードを閉じる方法
環境
- Swift
- Swift3.0
- UITextField
- シミュレーターキーボード
UITextFieldDelegate継承
class ViewController: UIViewController, UITextFieldDelegate {
UITextFieldを定義
@IBOutlet weak var textField: UITextField!
delegateをselfに設定
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textFieldShouldReturnを実装
// 改行ボタンを押した時の処理
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// キーボードを閉じる
textField.resignFirstResponder()
return true
}