UITextView没有使用键盘

时间:2017-08-04 17:05:41

标签: swift3 uiscrollview uitextview

我大部分时间都在努力解决这个问题,这是我的问题:

我有一个UIScrollView,里面有垂直堆栈视图,在堆栈视图内部我有几个包含不同内容的UIView。在底部视图中,我有一个UITextView,当我开始输入它时,我理想地想要向上移动,这样我才能真正看到我正在键入的内容。

如果我有{{1},那么实施找到的解决方案herehereherehere以及关于此主题的所有其他类似的先前问题都有效在最后一个视图中而不是UITextField,但我真的希望返回键/多行功能,这似乎需要UITextView

以下是我的代码(根据以上几个链接):

UITextView

我在func registerForKeyboardNotifications() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil) } func keyboardWasShown(_ notification: NSNotification) { guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } let keyboardFrame = keyboardFrameValue.cgRectValue let keyboardSize = keyboardFrame.size let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) scrollView.contentInset = contentInsets scrollView.scrollIndicatorInsets = contentInsets } func keyboardWillBeHidden(_ notification: NSNotification) { let contentInsets = UIEdgeInsets.zero scrollView.contentInset = contentInsets scrollView.scrollIndicatorInsets = contentInsets } 中调用registerForKeyboardNotifications(),当我使用viewDidLoad()时,这完全 ,但无论出于何种原因,功能都没有使用UITextField。我可以在我的问题中找到最接近的SO问题this one,但问题从未得到涉及UITextView的可行解决方案的回答。我看过几篇提到IQKeyboardManager的帖子,但我理想的想要解决这个问题。

1 个答案:

答案 0 :(得分:0)

我找到了一个结合了contentInsetsetContentOffset的解决方案,但我确定它并不是最优雅的解决方案。但是,如果其他人在使用textView获取keyBoardWasShown时遇到问题,您可以执行以下操作:

设置viewController以注意键盘出现的时间;

func registerForKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}

func keyboardWasShown(_ notification: NSNotification) {

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }

    let keyboardFrame = keyboardFrameValue.cgRectValue
    let keyboardSize = keyboardFrame.size

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

}

func keyboardWillBeHidden(_ notification: NSNotification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

务必致电registerForKeyboardNotifications()中的viewDidLoad

这取决于您的具体设置。由于我有一个view覆盖了大部分屏幕空间,我真的只希望我的scrollView(我做成IBOutlet)跳得足以显示我textView的一部分1}}以便用户知道它在那里并且可以编辑。这导致以下对处理键盘显示/隐藏状态的代码部分进行了以下更改;

func keyboardWasShown(_ notification: NSNotification) {

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }

    let keyboardFrame = keyboardFrameValue.cgRectValue
    let keyboardSize = keyboardFrame.size

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height - 50.0, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    let bottom = keyboardSize.height - 60.0
    scrollView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true)

}

func keyboardWillBeHidden(_ notification: NSNotification) {

    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

}

关键项目是scrollView.setContentOffset部分。