将scrollview移至原始状态

时间:2017-04-01 14:48:08

标签: ios

我想在键盘出现时向上移动视图,并在键盘消失时向后移动视图。我为此使用了一个scrollview。

我有这个几乎工作。我遇到的错误是:

  1. 触发键盘显示
  2. 触发键盘隐藏
  3. 到目前为止事情进展顺利。

    1. 触发键盘显示
    2. 你触发键盘隐藏< - 这不再起作用了,但第一次完美运作。
    3. 键盘出现时的代码(超时工作):

      func keyboardWasShown(notification: NSNotification) {
          var userInfo = notification.userInfo!
          let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
          let buttonOrigin: CGPoint = btn.frame.origin
          let buttonHeight: CGFloat = btn.frame.size.height
          var visibleRect: CGRect = view.frame
      
          visibleRect.size.height -= keyboardFrame.size.height
          if !visibleRect.contains(buttonOrigin) {
              let scrollPoint = CGPoint(x: CGFloat(0.0), y: CGFloat(buttonOrigin.y - visibleRect.size.height + (buttonHeight + 8)))
              scrollView.setContentOffset(scrollPoint, animated: true)
          }
      }
      

      键盘将要隐藏的代码(此代码第一次运行,第二次不运行):

      func keyboardWillBeHidden(notification: NSNotification){
          var info = notification.userInfo!
          let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
      
          let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
      
          self.scrollView.contentInset = contentInsets
          self.scrollView.scrollIndicatorInsets = contentInsets
          self.view.endEditing(true)
          self.scrollView.isScrollEnabled = false
      }
      

      这就是我的观点: enter image description here

      键盘显示时,一切都需要向上移动。向上移动:

      enter image description here

      但是当键盘消失时它不起作用: enter image description here

2 个答案:

答案 0 :(得分:2)

只需更改此行
 let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)

let contentInsets : UIEdgeInsets = .zero

或试试这个

// MARK: - 键盘方法

  func keyboardWillOpen(sender:Notification) {

        if let info = sender.userInfo{

            if let keyboardSize = info[UIKeyboardFrameBeginUserInfoKey] as? CGRect{
                let contentInsets = UIEdgeInsetsMake(0, 0, -keyboardSize.height, 0)


            self.scrollView.contentInset = contentInsets
            self.scrollView.scrollIndicatorInsets = contentInsets
            }
        }
    }

    func keyboardWillHide(notification:Notification) {

        if let info = notification.userInfo {

            if let keyboardSize = info[UIKeyboardFrameBeginUserInfoKey] as? CGRect {

            self.scrollView.contentInset = .zero
            self.scrollView.scrollIndicatorInsets = .zero

            }
        }
    }

答案 1 :(得分:0)

我不打算滚动滚动视图,而是移动滚动视图本身。

获取scrollview底部约束的出口,并且,假设滚动视图是全屏的,在隐藏键盘时将约束设置为零,然后将其设置为键盘高度(正确的值在keyboardwillshow的通知中)。

伪代码如下所示:

func keyboardWillShow(notification: NSNotification) {
    self.scrollviewBottomConstraint.constant = notification.keyboardheight;
    self.layoutifneeded(); //I suggest you put this in a 0.3 second animation block
}

func keyboardWillHide(notification: NSNotification) {
    self.scrollviewBottomConstraint.constant = 0;
    self.layoutIfNeeded(); //I suggest you put this in a 0.3 second animation block
}