iPhone X keyboard appear showing extra space

后端 未结 6 929
别那么骄傲
别那么骄傲 2020-12-14 07:54

I have created a chat UI in which I have added a constraint for the tableView to the bottom of the screen. I am changing the constraint value by ad

6条回答
  •  情歌与酒
    2020-12-14 08:39

    My issue was that my view was in a child view controller. Converting the CGRect did the trick.

    @objc private func keyboardWillChangeFrame(notification: NSNotification) {
        guard let userInfo = notification.userInfo, let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let duration: TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else {
            return
        }
    
        let convertedFrame = view.convert(endFrame, from: nil)
        let endFrameY = endFrame.origin.y
    
        let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
        let animationCurve: UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
    
        if endFrameY >= UIScreen.main.bounds.size.height {
            inputContainerViewBottomLayoutConstraint.constant = 0.0
        }  else {
            var newHeight = view.bounds.size.height - convertedFrame.origin.y
            if #available(iOS 11.0, *) {
                newHeight = newHeight - view.safeAreaInsets.bottom
            }
            inputContainerViewBottomLayoutConstraint.constant = newHeight
        }
    
        UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
            self.view.layoutIfNeeded()
        },completion: { _ in
            self.scrollView.scrollToBottom()
        })
    }
    

提交回复
热议问题