iPhone X keyboard appear showing extra space

后端 未结 6 914
别那么骄傲
别那么骄傲 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条回答
  •  -上瘾入骨i
    2020-12-14 08:35

    This worked for me

    TL;DR: self.view.safeAreaInsets.bottom returned 0. The key was to use UIApplication.shared.keyWindow.safeAreaInsets.bottom instead [Source].

    Let replyField be the UITextField of interest.

    1) Add the observers in viewDidLoad().

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    

    2) Set global variables within the class as the constraints.

    var textFieldBottomConstraintKeyboardActive : NSLayoutConstraint?
    var textFieldBottomConstraintKeyboardInactive : NSLayoutConstraint?
    

    3) Set the constraints in a function called in viewDidLoad.

    let replyFieldKeyboardActiveConstraint = self.replyField.bottomAnchor.constraint(
        equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,
        constant: -1 * Constants.DEFAULT_KEYBOARD_HEIGHT /* whatever you want, we will change with actual height later */ + UITabBar.appearance().frame.size.height
    )
    
    let replyFieldKeyboardInactiveConstraint = self.replyField.bottomAnchor.constraint(
        equalTo: self.view.safeAreaLayoutGuide.bottomAnchor
    )
    
    self.textFieldBottomConstraintKeyboardActive = replyFieldKeyboardActiveConstraint
    self.textFieldBottomConstraintKeyboardInactive = replyFieldKeyboardInactiveConstraint
    
    self.textFieldBottomConstraintKeyboardActive?.isActive = false
    self.textFieldBottomConstraintKeyboardInactive?.isActive = true
    

    4) Define the keyboardWillShow and keyboardWillHide methods. They key here is how we define the heightOffset in the keyboardWillShow method.

    @objc func keyboardWillShow(notification: NSNotification) {
        guard let userinfo = notification.userInfo else {
            return
        }
        guard let keyboardSize = userinfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }
        let keyboardFrame = keyboardSize.cgRectValue
    
        self.view.layoutIfNeeded()
    
        let heightOffset = keyboardFrame.height - UITabBar.appearance().frame.height - (UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0)
    
        self.textFieldBottomConstraintKeyboardActive?.constant = -1 * heightOffset
    
        self.textFieldBottomConstraintKeyboardActive?.isActive = true
        self.textFieldBottomConstraintKeyboardInactive?.isActive = false
    
        self.view.setNeedsLayout()
    }
    
    
    @objc func keyboardWillHide(notification: NSNotification) {
        self.textFieldBottomConstraintKeyboardActive?.isActive = false
        self.textFieldBottomConstraintKeyboardInactive?.isActive = true
    }
    

提交回复
热议问题