Move ScrollView when keyboard show/hide

前置作業

你必須有UITextField然後他的位置在偏下面

鍵盤收起

有兩種做法

  1. 鍵盤內建的return鍵收起
  2. 點擊鍵盤外畫面收起
return是指這個(但使用者不一定知道這個按下去會收起來)

可透過UITextFieldDelegate的textFieldShouldReturn收起鍵盤

但記得將UITextField的代理人(delegate)設成自己(ViewController)

使用點擊判斷動作來收起鍵盤

endEditing只要有找到該view上有textField就會強制resign

根據UITextField位置讓鍵盤移動

設定NotificationCenter

  • UIResponder.keyboardWillShowNotification(鍵盤即將顯示)
  • UIResponder.keyboardWillHideNotification(鍵盤即將消失)

--

--

On my way to become an iOS developer!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store