2017-04-01 76 views
1

我想在键盘出现时向上移动视图,并在键盘消失时将视图移回。我为此使用了一个滚动视图。将scrollview移动到原始状态

我有这个几乎工作。我有这个bug是这样的:

  1. 你触发键盘显示
  2. 你触发键盘隐藏

生活还算不错至今。

  • 您触发键盘显示
  • 你触发键盘隐藏< - 这不工作了,但是它第一次完美的作品。
  • 当键盘显示出来的代码(这个超时工作):

    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

    +0

    尝试使用该吊舱'IQKeyboardManagerSwift'is真棒 –

    回答

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

    这不起作用,屏幕并没有移动。 – da1lbi3

    +0

    @ da1lbi3请尝试更新的答案,并让我知道它是否正常工作 –

    +0

    您更新的答案不起作用。两种方法都没有发生。 – da1lbi3

    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 
    }