我的方法是基于听取键盘通知,然后通过additionalSafeAreaInsets调整安全区域.
这是我的代码:
overrIDe func vIEwDIDLoad() { let notificationCenter = NotificationCenter.default notificationCenter.addobserver(self,selector: #selector(keyboarDWillShow(notification:)),name: NSNotification.name.UIKeyboarDWillShow,object: nil) notificationCenter.addobserver(self,selector: #selector(keyboarDWillHIDe(notification:)),name: NSNotification.name.UIKeyboarDWillHIDe,selector: #selector(keyboarDWillChange(notification:)),name: NSNotification.name.UIKeyboarDWillChangeFrame,object: nil)}//MARK: - Keyboardextension MyVIEwController { @objc func keyboarDWillShow(notification: NSNotification) { let userInfo = notification.userInfo! let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height additionalSafeAreaInsets = UIEdgeInsets(top: 0,left: 0,bottom: keyboardHeight,right: 0) UIVIEw.animate(withDuration: 0.3) { self.vIEw.layoutIfNeeded(); } } @objc func keyboarDWillHIDe(notification: NSNotification) { additionalSafeAreaInsets = UIEdgeInsets(top: 0,bottom: 0,right: 0) UIVIEw.animate(withDuration: 0.3) { self.vIEw.layoutIfNeeded(); } } @objc func keyboarDWillChange(notification: NSNotification) { let userInfo = notification.userInfo! let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height additionalSafeAreaInsets = UIEdgeInsets(top: 0,right: 0) UIVIEw.animate(withDuration: 0.3) { self.vIEw.layoutIfNeeded(); } }}
这很好用,因为MyController是一个带有UItableVIEw的UIVIEwController,它可以扩展到整个安全区域.现在当键盘出现时,底部被向上推,因此键盘后面没有单元格.
问题在于底栏.我底部还有一个工具栏,已经包含在安全区域中.因此,将全键盘高度设置为额外的安全区域插入会将表格视图的底部向上推动过多地与底栏的高度相对应.为了使这个方法运行良好,我必须将additionalSafeAreaInsets.bottom设置为等于键盘高度减去底栏的高度.
问题1:在底部获得当前安全区间隙的最佳方法是什么?手动获取工具栏框架并使用其高度?或者是否可以直接从安全区域布局指南中获得差距?
问题2:据推测,如果没有键盘改变大小,底栏应该可以改变大小,所以我也应该实现一些方法来监听栏中框架的变化.这是最好的在vIEwWillTransition中完成(大小:CGSize,与协调员:UIVIEwControllerTransitionCoordinator)?或其他地方?
谢谢
@H_403_18@解决方法 对我来说似乎有用的是计算vIEw.safeAreaLayoutGuIDe.layoutFrame和键盘框架之间的交集,然后将其高度设置为additionalSafeAreaInsets.bottom,而不是整个键盘框架高度.我的视图控制器中没有工具栏,但我有一个标签栏,并且它被正确计算.完整代码:
extension UIVIEwController { func startAvoIDingKeyboard() { NotificationCenter.default.addobserver(self,selector: #selector(_onKeyboardFrameWillChangeNotificationReceived(_:)),object: nil) } func stopAvoIDingKeyboard() { NotificationCenter.default.removeObserver(self,object: nil) } @objc private func _onKeyboardFrameWillChangeNotificationReceived(_ notification: Notification) { guard let userInfo = notification.userInfo,let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } let keyboardFrameInVIEw = vIEw.convert(keyboardFrame,from: nil) let safeAreaFrame = vIEw.safeAreaLayoutGuIDe.layoutFrame.insetBy(dx: 0,dy: -additionalSafeAreaInsets.bottom) let intersection = safeAreaFrame.intersection(keyboardFrameInVIEw) let animationDuration: TimeInterval = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 let animationCurveRawNSN = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIVIEwAnimationoptions.curveEaseInOut.rawValue let animationCurve = UIVIEwAnimationoptions(rawValue: animationCurveRaw) UIVIEw.animate(withDuration: animationDuration,delay: 0,options: animationCurve,animations: { self.additionalSafeAreaInsets.bottom = intersection.height self.vIEw.layoutIfNeeded() },completion: nil) }}@H_403_18@ @H_403_18@ 总结
以上是内存溢出为你收集整理的扩展iOS 11安全区域以包括键盘全部内容,希望文章能够帮你解决扩展iOS 11安全区域以包括键盘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)