Adjusting UITextView frame for using UIKeyboardFrameEndUserInfoKey not working

I am trying to just the UITextView in my ViewController so when the keyboard appears the UITextView is not obstructed by the keyboard.

I have already successfully done this before with the code below, but since the iPhone X came out my app now only displays the UITextView correctly on the iPhone X and on other devices like the iPhone it obstructs the UITextView.

NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)


@objc func adjustForKeyboard(_ notification: Notification) {
    let userInfo = notification.userInfo!


    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)

    if notification.name == Notification.Name.UIKeyboardWillHide {
        self.textView.contentInset = UIEdgeInsets.zero
    } else {
        self.textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    self.textView.scrollIndicatorInsets = self.textView.contentInset

    let selectedRange = self.textView.selectedRange
    self.textView.scrollRangeToVisible(selectedRange)

    print("Keyboard End Frame = \(keyboardScreenEndFrame) and Keyboard View End Frame = \(keyboardViewEndFrame)")
}

Console

Running On iPhone X

Keyboard End Frame = (0.0, 479.0, 375.0, 333.0) and Keyboard View End Frame = (0.0, 479.0, 375.0, 333.0)

Running On iPhone 8

Keyboard End Frame = (0.0, 409.0, 375.0, 258.0) and Keyboard View End Frame = (0.0, 409.0, 375.0, 258.0)

enter image description here

Does anybody have any ideas what might be going wrong ?

1 answer

  • answered 2018-01-13 17:55 Sh_Khan

    Hook bottom constraint of textView as IBOutlet and do this

    @objc func adjustForKeyboard(_ notification: Notification) {
        let userInfo = notification.userInfo!         
        let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
    
        if notification.name == Notification.Name.UIKeyboardWillHide {
            self.textViewBottomCon.constant = 0
        } else {
             self.textViewBottomCon.constant = -1 * ( keyboardViewEndFrame.height + 20.0 )
        } 
        self.view.layoutIfNeeded() 
    }
    

    Also remove these 2 observers

    NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.adjustForKeyboard(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)