【问题标题】:scrollView problem with UITextView when keyboard hide/show notification键盘隐藏/显示通知时 UITextView 的滚动视图问题
【发布时间】:2020-07-10 14:25:42
【问题描述】:

我的 viewController 中有多个 textFieldtextViewscrollView 中。我用这些代码处理键盘显示和隐藏:

我在viewDidLoad中添加了这行代码:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:UIResponder.keyboardWillHideNotification, object: nil)

还有这两个功能:

@objc func keyboardWillShow(notification:NSNotification){
    guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
        
    let keyboardScreenEndFrame = keyboardValue.cgRectValue
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
    let bottom = keyboardViewEndFrame.height - view.safeAreaInsets.bottom + 16
        
    self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: bottom , right: 0)
}
    
@objc func keyboardWillHide(notification:NSNotification){
    self.scrollView.contentInset = UIEdgeInsets.zero
}

当我开始编辑文本字段时一切正常。但它不适用于textView,并且无法滚动到活动的 textView。

我该如何解决?

【问题讨论】:

    标签: swift uiscrollview keyboard-events


    【解决方案1】:

    这个问题的原因在here进行了解释,所以如果你想在UIScrollView中使用UITextView,那么从右边的菜单检查器中取消选中Scrolling Enabled或从代码中设置它False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-19
      • 2013-01-22
      • 1970-01-01
      • 2018-01-09
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多