在iOS 13中,键盘消失后,safeAreaInsets的值会发生变化。

9 浏览
0 Comments

在iOS 13中,键盘消失后,safeAreaInsets的值会发生变化。

我有一个bottomView(图像中带有黑色边框的那个),它的底部锚点与底部视图的safeAreaLayoutGuide有一个约束,但是当键盘将要显示时,bottomView变成了键盘的工具栏。

在iOS 14中,一切都按预期工作(如图像1和3),但是在iOS 13中运行项目时,我发现在键盘消失后safeAreaInsets的值发生了变化(红色空间增加了高度,如图像2所示)。

我尝试了一些在这里提到的解决方案,但没有帮助。

有人能提供一个解决方案吗?

@objc func adjustForKeyboard(notification: Notification) {

guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }

let keyboardScreenEndFrame = keyboardValue.cgRectValue

let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)

if notification.name == UIResponder.keyboardWillHideNotification {

bottomViewConstraint?.constant = 0

NotesText.contentInset = .zero

} else {

bottomViewConstraint?.constant = -(keyboardViewEndFrame.height - self.view.safeAreaInsets.bottom)

NotesText.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -(keyboardViewEndFrame.height + bottomViewConstraint!.constant), right: 0)

//FIXME:- force unwrap

}

NotesText.scrollIndicatorInsets = NotesText.contentInset

let selectedRange = NotesText.selectedRange

NotesText.scrollRangeToVisible(selectedRange)

}

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

addKeyboardNotification()

// 默认显示键盘

NotesText.becomeFirstResponder()

}

fileprivate func addKeyboardNotification() {

let notificationCenter = NotificationCenter.default

notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)

notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}

我在viewDidAppear中调用了这个函数,还尝试在viewSafeAreaInsetsDidChange中调用它,但行为没有改变。

[图1](https://i.stack.imgur.com/qK4qN.png)

[图2](https://i.stack.imgur.com/UX4fV.png)

[图3](https://i.stack.imgur.com/i5o2h.png)

0