1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

キーボード表示時に取得したサイズの誤り

Posted at

キーボードが表示された時に UITextView の下位置を調整しようとした際に
最初は cgSizeValue で height を取得していたため位置の調整に失敗していた。
cgRectValue で取得でないとダメ。念の為取得結果を検証してみました。

キーボード表示時

@objc func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
       self.textViewBottom.constant = keyboardFrame.cgRectValue.height

            let keyboardRect = keyboardFrame.cgRectValue
            let keyboardSize = keyboardFrame.cgSizeValue
            let keyboardPoint = keyboardFrame.cgPointValue
            
            print("self.view.frame.size[\(self.view.frame.size)] self.view.frame.origin[\(self.view.frame.origin)]")
            print("keyboardFrame.cgRectValue.size[\(keyboardRect.size)] cgRectValue.cgRectValue.origin[\(keyboardRect.origin)]")
            print("keyboardFrame.cgSizeValue[\(keyboardSize)]")
            print("keyboardFrame.cgPointValue[\(keyboardPoint)]")
        }
    }
}

iPhone8 で実行

self.view.frame.size[(375.0, 667.0)] self.view.frame.origin[(0.0, 0.0)]
keyboardFrame.cgRectValue.size[(375.0, 260.0)] cgRectValue.cgRectValue.origin[(0.0, 407.0)]
keyboardFrame.cgSizeValue[(0.0, 407.0)]
keyboardFrame.cgPointValue[(0.0, 407.0)]

cgSizeValue は cgPointValue の値と同じなってしまっているようで、バグっぽいですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?