【发布时间】:2018-09-30 23:35:28
【问题描述】:
当用户单击它时,我正在使用以下方法将我的“UITextView”向上移动。但这适用于 iPhone 7 和 iPhone X,但是当我在 iPhone 6、5s 上尝试时,textView 没有向上移动。 请帮助我以正确的方式向上移动键盘。
extension UIViewController {
func startAvoidingKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(_onKeyboardFrameWillChangeNotificationReceived(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
func stopAvoidingKeyboard() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc private func _onKeyboardFrameWillChangeNotificationReceived(_ notification: Notification) {
if #available(iOS 11.0, *) {
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)
}
}
}
【问题讨论】:
-
为此使用 IQKeyboardManager 库...最好
-
6s和5的操作系统版本是多少?
-
请检查代码,有括号错误...
-
@IgnacioAra 你能告诉我在哪里吗?
-
@arash iPhone 6s 和 5 都在 iOS 11 上
标签: ios swift uitextfield uitextview uikeyboard