当textView显示键盘不起作用时向上移动视图

时间:2018-01-05 11:08:34

标签: ios swift keyboard textview uitextfield

我在故事板中有一个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时调用的方法和滚动视图内容插入与点击文本字段时相同,但视图不会向上移动。 怎么能解决这个问题呢?

enter image description here

1 个答案:

答案 0 :(得分:1)

INTEGER PRIMARY KEY AUTOINCREMENT
相关问题