如何解决在 iOS 13 中关闭键盘后,safeAreaInsets 更改其值
我有 bottomView(图像中带有黑色边框的那个)它的底部锚点对底部视图 safeAreaLayoutGuide 有约束,但是当键盘显示时,bottomView 变成了键盘的工具栏。
这里的一切都在 iOS 14 中按预期工作(如图像 1 和 3)但是在 iOS 13 中运行项目时,我发现 safeAreaInsets 在键盘关闭(红色空间增加其高度,如图像 2)
我尝试了一些提到 here 的解决方案,但没有帮助 任何人都可以为此提出解决方案吗?
@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)
}
并在 viewDidAppear 中调用此函数也尝试在 viewSafeAreaInsetsDidChange 中调用它但行为没有改变
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addKeyboardNotification()
// Show keyboard by default
NotesText.becomeFirstResponder()
}
fileprivate func addKeyboardNotification() {
let notificationCenter = NotificationCenter.default
notificationCenter.addobserver(self,selector: #selector(adjustForKeyboard),name: UIResponder.keyboardWillHideNotification,object: nil)
notificationCenter.addobserver(self,name: UIResponder.keyboardWillChangeFrameNotification,object: nil)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。