当键盘覆盖 UITextView 时如何移动它

Way*_*yne 5 uitextview ios swift

在此输入图像描述

UITextView当需要一些输入时,我需要将键盘移开。我正在使用以下代码,它与 a 完美配合,UITextField但与 a 则根本不起作用UITextView

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(DailyNotesViewController.keyboardWillShow(_:)),
                                                     name: UIKeyboardWillShowNotification,
                                                     object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(DailyNotesViewController.keyboardWillHide(_:)),
                                                     name: UIKeyboardWillHideNotification,
                                                     object: nil)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
    let userInfo = notification.userInfo ?? [:]
    let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
    scrollView.contentInset.bottom += adjustmentHeight
    scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}

func keyboardWillShow(notification: NSNotification) {
    adjustInsetForKeyboardShow(true, notification: notification)
}

func keyboardWillHide(notification: NSNotification) {
    adjustInsetForKeyboardShow(false, notification: notification)
}
Run Code Online (Sandbox Code Playgroud)

请建议我缺少什么?

Way*_*yne 5

实际的解决方案非常简单。

在 UITextView 上,将scrollingEnabled 设置为 False。

感谢您的所有帮助。