Kyle Beard
Kyle Beard

Reputation: 664

iPhone X Keyboard Notification Height Differs

I've had this strange occurrence recently when I've been using keyboardWillShow and keyboardWillShow where the initial call of getting the height of the keyboard from (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue returns a value of 477px and then all other time afterwards it says the value is now 535px, which is 58px larger. However, visually, the keyboard hasn't changed in appearance whatsoever. The predictive bar is enabled for both keyboards.

As background info, the purpose of needing the keyboard height is to offset the scroll of a tableview in which each cell contains a textfield and I'm comparing the position of the textfield to see if it's hidden behind the keyboard when editing begins.

Am I missing something in how my methodology of understanding this works?

Upvotes: 4

Views: 759

Answers (2)

Latenec
Latenec

Reputation: 418

I just faced the similar scenario and the solution was basically this:

func keyboardWillShow(_ notification: Notification) {
    guard
        let userInfo = notification.userInfo,
        let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
        let keyboardEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    else { return }

    let window = UIApplication.shared.keyWindow
    let bottomPadding = window?.safeAreaInsets.bottom ?? 0.0 // This is the key
    buttonConstraint.constant = keyboardEndFrame.height - bottomPadding
    UIView.animate(withDuration: animationDuration) { [weak self] in
        self?.view.layoutIfNeeded()
    }
}

iPhone X (and other devices with OLED) has safeAreaInsets, which is added in your case.

Upvotes: 1

sudarvizhi s
sudarvizhi s

Reputation: 48

May this help you it is working for me fine

 NotificationCenter.default.addObserver(self, selector: #selector(CommentsVC.keyboardWillShow), name: 
 NSNotification.Name.UIKeyboardWillShow, object: nil)
                    NotificationCenter.default.addObserver(self, selector: #selector(CommentsVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

@objc func keyboardWillHide(_ notification: NSNotification) {
                UIView.animate(withDuration: 0.3) {
                    self.inputContainerViewBottom.constant = 0


                    self.view.layoutIfNeeded()


                }
            }


@objc func keyboardWillShow(_ notification: NSNotification) {
                print(notification)
                let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
                UIView.animate(withDuration: 0.3) {
                    self.inputContainerViewBottom.constant = keyboardFrame!.height

                    self.view.layoutIfNeeded()


                    let flag = self.tableComments.isCellVisible(section: 0, row: 10 - 1)

                    if flag
                    {
                        self.scrollToBottom()
                    }
                    else
                    {

                    }

                }
            }

Note: inputContainerViewBottom is the outlet of bottom constraint

Upvotes: 1

Related Questions