问题描述
我使用这个 example 来避免出现键盘框架。现在我想让它基于协议,以避免使用此功能扩展所有视图控制器。现在我收到两个看起来相互冲突的错误(一个是想添加@objc,另一个是删除它):
我的代码:
import UIKit
protocol KeyboardFrameIgnorable {
func listenForKeyboardFrameChange()
func stopListenKeyboardFrameChange()
}
extension KeyboardFrameIgnorable where Self: UIViewController {
func listenForKeyboardFrameChange()
{
NotificationCenter.default
.addobserver(self,selector: #selector(onKeyboardFrameWillChangeNotificationReceived(_:)),name: UIResponder.keyboardWillChangeFrameNotification,object: nil)
}
func stopListenKeyboardFrameChange()
{
NotificationCenter.default
.removeObserver(self,object: nil)
}
@objc
private func onKeyboardFrameWillChangeNotificationReceived(_ notification: Notification)
{
guard
let userInfo = notification.userInfo,let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] 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 keyboardAnimationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey]
let animationDuration: TimeInterval = (keyboardAnimationDuration as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.Animationoptions.curveEaseInOut.rawValue
let animationCurve = UIView.Animationoptions(rawValue: animationCurveRaw)
UIView.animate(withDuration: animationDuration,delay: 0,options: animationCurve,animations: {
self.additionalSafeAreaInsets.bottom = intersection.height + 10
self.view.layoutIfNeeded()
},completion: nil)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)