Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

キーボードが表示された時に 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 の値と同じなってしまっているようで、バグっぽいですね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away