LoginSignup
14
7

More than 5 years have passed since last update.

Swift 3 での NotificationCenter の書き方、キーボードの表示を検知したい

Last updated at Posted at 2016-09-22

Swift 2系の情報が多く 3 の情報が少なかったのでメモ
ViewContoroller に以下のように記述


override func viewDidLoad() {
    super.viewDidLoad()
    let nofitiShow = Notification.Name.UIKeyboardWillShow
    let nofitiHide = Notification.Name.UIKeyboardWillHide

    // Notification の追加       
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.keyboardWillShow(notification:)),
        name: nofitiShow,
        object: nil
    )
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.keyboardWillHide(notification:)),
        name: nofitiHide,
        object: nil
    )
}

// キーボード出現時
func keyboardWillShow(notification: NSNotification) {
    print("open")
}

// キーボード閉じる時
func keyboardWillHide(notification: NSNotification) {
    print("close")
}

これでテキストフィールドがキーボードで隠れてしまう時の処理が書ける!

14
7
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
14
7