调整 UITextView 框架以使用 UIKeyboardFrameEndUserInfoKey 不起作用

Adjusting UITextView frame for using UIKeyboardFrameEndUserInfoKey not working

我正在尝试只在我的 ViewController 中设置 UITextView,因此当键盘出现时,UITextView 不会被键盘挡住。

我之前已经用下面的代码成功完成了这个,但是自从 iPhone X 出来后,我的应用程序现在只在 iPhone X 上正确显示 UITextView iPhone 等其他设备会阻碍 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)")
}

控制台

运行 在 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)

运行 在 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)

有没有人知道可能出了什么问题?

将 textView 的底部约束挂钩为 IBOutlet 并执行此操作

@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() 
}

同时移除这 2 个观察者

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)