【问题标题】:Move up view when textView show keyboard not working当 textView 显示键盘不起作用时上移视图
【发布时间】:2018-06-15 04:36:23
【问题描述】:

我在情节提要中有一个 textView 和 textfield。我想在 textfield 或 textView 显示键盘时向上移动滚动视图。对于 UITextField 来说,每件事都可以正常工作,但对于 textView 来说,那是行不通的。我有向上移动视图的代码:

  func keyboardWillShow(notification: NSNotification) {

    if let endFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
      var keyboardHeight = view.bounds.height - endFrame.origin.y
      if #available(iOS 11, *) {
        if keyboardHeight > 0 {
          keyboardHeight = keyboardHeight - view.safeAreaInsets.bottom
          var contentInset:UIEdgeInsets = self.scrollView.contentInset
              contentInset.bottom = keyboardHeight
              viewBottom.constant = keyboardHeight
              self.scrollView.contentInset = contentInset
        }
      }
      else {
            let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
            var contentInset:UIEdgeInsets = self.scrollView.contentInset
            contentInset.bottom = (keyboardSize.height)
            self.scrollView.contentInset = contentInset
      }
      view.layoutIfNeeded()
    }

  }

  func keyboardWillHide(notification: NSNotification) {

    var contentInset:UIEdgeInsets = self.scrollView.contentInset
    contentInset.bottom = 0
    self.scrollView.contentInset = contentInset

  }

用于显示键盘的 textfield 和 textView 有什么区别? 当我点击 textview 时调用的方法和滚动视图内容插入与点击文本字段但视图不向上移动时相同。 如何解决这个问题?

【问题讨论】:

  • 你弄明白了吗?
  • @Do2 我无法管理它。现在我正在使用 IQKeyboardManagerSwift pod。
  • 检查我的答案...

标签: ios swift keyboard textview uitextfield


【解决方案1】:
class matchViewController: UIViewController,UITextViewDelegate {
var activeTextview: UITextView?
@IBOutlet weak var yourTextview: UITextView!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    yourTextview.delegate=self
    NotificationCenter.default.addObserver(self, selector: #selector(yourViewController.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.addObserver(self, selector: #selector(yourViewController.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: self.view.window)

}

@objc func keyboardWillShow(_ sender: Notification) {

    var info: [AnyHashable : Any] = sender.userInfo!
    let kbSize: CGSize = ((info[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue.size)
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
    var aRect: CGRect = self.view.frame
    aRect.size.height -= kbSize.height

    if let activeTextview=activeTextview{
        self.scrollView.scrollRectToVisible((activeTextview.frame), animated: true)

    }

}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, with: event)
}

@objc func keyboardWillHide(_ sender: Notification) {
    let userInfo: [AnyHashable: Any] = sender.userInfo!
    let keyboardSize: CGSize = (userInfo[UIKeyboardFrameBeginUserInfoKey]! as AnyObject).cgRectValue.size
    self.scrollView.frame.origin.y += keyboardSize.height


}


override func viewDidDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}



  func textViewDidBeginEditing(_ textView: UITextView) {
    activeTextview=textView

}
func textViewDidEndEditing(_ textView: UITextView){
    activeTextview=nil
}
}

【讨论】:

  • 用你的代码隐藏键盘后将我的视图绘制到底部。我将 self.scrollView.frame.origin.y += keyboardSize.height 更改为 self.scrollView.frame.origin.y = 0,这工作正常。谢谢回复
猜你喜欢
  • 1970-01-01
  • 2019-01-10
  • 2023-03-09
  • 1970-01-01
  • 2014-11-23
  • 2015-10-19
  • 1970-01-01
  • 2015-05-31
  • 2018-12-16
相关资源
最近更新 更多