LoginSignup
1
2

More than 3 years have passed since last update.

キーボードの高さを取得する

Posted at

よく使うけど変換の仕方を忘れがちなので、備忘録として残しておく。

@objc func keyboardDidChangeFrame(_ note: Notification) {

        guard
            let value: Any = note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey],
            let frameValue: NSValue = value as? NSValue else
        { return }
        let frame: CGRect = frameValue.cgRectValue
        // https://developer.apple.com/documentation/uikit/uicoordinatespace/1622661-convert
        let convertedFrame: CGRect = view.convert(frameEnd, from: UIScreen.main.coordinateSpace)
        // https://developer.apple.com/documentation/coregraphics/cgrect/1455346-intersection
        let height: CGFloat = view.frame.intersection(convertedFrame).height

        // スクロールビューのインセットを調整したりなど
    }

参考

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