我在故事板中有一个文本视图和文本域。当文本域或文本视图显示键盘时,我想向上移动滚动视图。对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的方法调用和滚动视图内容插入时,与点击textfield相同,但视图不向上移动。
如何解决这个问题?
ios - 当textView显示键盘不起作用时,向上移动 View-LMLPHP

最佳答案

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
}
}

08-18 08:03