【问题标题】:Move Keyboard Up Issue on iOS 11 iPhone 6 are earlier modeliOS 11 iPhone 6 上的上移键盘问题是早期型号
【发布时间】: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


【解决方案1】:

在 iOS 11 及更高版本中,您的代码可以正常工作,但对于早期版本,您应该使用 NSNotification.Name.UIKeyboardDidChangeFrame 而不是 NSNotification.Name.UIKeyboardWillChangeFrame

【讨论】:

  • 你检查过 DidChangeFrame 吗?
  • 什么?如何检查?
  • 忽略操作系统版本,照我说的做,检查结果
  • 检查什么?
  • 检查 textview 是否向上移动
【解决方案2】:

请检查 iPhone 6、5s 的版本。

仅在版本等于 11.0 或更高版本时才有效

【讨论】:

  • 是的,它在 iOS 11 上
【解决方案3】:

你为什么要那样做? 我建议您每次触摸“UITextView”时都使用约束动画来为您的约束设置动画(以便它移动),您可以阅读我给其他人关于如何为约束设置动画的答案(here

现在您听说过 CocoaPods 吗?您可以使用一些有用的库来为您的应用程序制作动画(例如HERO)。

如果您不知道什么是可可豆荚或如何安装它,请观看此视频 (Cocoa Pods Tutorial - How to install and setup Cocoa Pods)

有时你也想在一种设备上做特定的动画或特定的事情(不管你脑子里有什么)(比如说只有 iPhone 8,别无其他);为此你可以使用Device Kit 我建议你阅读它

希望能帮到你

谢谢

【讨论】:

    【解决方案4】:
    func keyboardWillShow(_ notification: Notification)
    {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            let info: NSDictionary = notification.userInfo! as NSDictionary
            let keyboardHeight = ((info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size.height)! + self.extraPadding
    
            let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0)
            self.scrollView?.contentInset = contentInsets
            self.scrollView?.scrollIndicatorInsets = contentInsets
        }
    }
    
    func keyboardWillBeHidden(_ notification: Notification)
    {
        let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
        scrollView?.contentInset = contentInsets
        scrollView?.scrollIndicatorInsets = contentInsets
    }
    

    这是我在显示/隐藏键盘时调整滚动视图的内容区域的方式。您应该能够以相同的方式调整 UITextView 的内容区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-28
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2013-08-03
      • 2014-11-14
      • 1970-01-01
      相关资源
      最近更新 更多