LoginSignup
12
11

More than 5 years have passed since last update.

[iOS]フォーカスが外れたタイミングでキーボードを隠す

Posted at

何もしないでも標準キーボードはフォーカスが外れたら自動的に隠れてくれるものだと思っていたけど何もしてくれないみたいなので。

resignFirstResponderを使ったりDid End On Exitを使ったり方法があるみたいだけど、
どのテキストフィールドでもフォーカス外れたらキーボード隠すならこれが一番シンプルそう。

hideKeyboard.swift
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var textField: UITextField!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        view.endEditing(true)
    }
}
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11