【问题标题】:Scroll to the very bottom of UIScrollView when Keyboard appears (AutoLayout)出现键盘时滚动到 UIScrollView 的最底部(自动布局)
【发布时间】:2015-08-01 02:18:41
【问题描述】:

我试图在显示键盘时滚动到 UIScrollView 的底部内容,但 setContentOffset 方法似乎不起作用。我在 Interface Builder 上使用 AutoLayout。也许这就是导致问题的原因?有什么想法吗?

当我的键盘显示时,我如何滚动到 UIScrollView 的底部。

P.S.:它会很好地显示 textField,我需要的是在显示键盘时显示 UIScrollView 的最底部

谢谢!!

下面是我当前的代码

func textFieldDidBeginEditing(textField: UITextField) {
    var bottomOffset: CGPoint = CGPoint(x: 0, y: mainScrollView.contentSize.height - mainScrollView.bounds.size.height);
    mainScrollView.setContentOffset(bottomOffset, animated: true);
}

func keyboardDidShow(notification: NSNotification) {
    var userInfoDictionary: NSDictionary = notification.userInfo!;

    var keyboardSize: CGSize = userInfoDictionary.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size;

    var contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0);

    mainScrollView.contentInset = contentInsets;
    mainScrollView.scrollIndicatorInsets = contentInsets;
}

func keyboardWillHide(notification: NSNotification) {
    var contentInsets = UIEdgeInsetsZero;

    mainScrollView.contentInset = contentInsets;
    mainScrollView.scrollIndicatorInsets = contentInsets;
}

【问题讨论】:

    标签: ios swift uiscrollview uikeyboard


    【解决方案1】:

    对我来说,滚动到滚动视图的底部意味着设置 偏移量 等于 总内容高度 减去实际的滚动视图高度 .

    例如,要在键盘出现时滚动到 UIScrollView 的底部,我使用以下代码:

    目标-C

    - (void)onKeyboardDidShow:(NSNotification *)notification {
        NSValue *value = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGFloat keyboardHeight = value.CGRectValue.size.height;
        self.scrollViewBottomConstraint.constant = keyboardHeight;
        CGPoint offset = self.scrollView.contentOffset;
        CGFloat scrollViewHeight = self.scrollView.frame.size.height - keyboardHeight;
        offset.y = self.scrollView.contentSize.height - scrollViewHeight;
        [self.scrollView setContentOffset:offset animated:YES];
    }
    

    斯威夫特 3

    func onKeyboardDidShow(notification: NSNotification) {
        if let value = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] {
            let keyboardHeight = (value as AnyObject).cgRectValue.size.height
            self.scrollViewBottomConstraint.constant = keyboardHeight
            var offset = self.scrollView.contentOffset
            let scrollViewHeight = self.scrollView.frame.size.height - keyboardHeight
            offset.y = self.scrollView.contentSize.height - scrollViewHeight
            self.scrollView.setContentOffset(offset, animated: true)
        }
    }
    

    底部约束出现时变为键盘高度,消失时变为0。

    有趣的是,滚动视图的实际高度不仅仅是self.scrollView.frame.size.height(至少在键盘刚出现时)。我必须减去键盘尺寸才能获得真正的价值。

    【讨论】:

      【解决方案2】:

      我遇到了与原始发帖人完全相同的问题,但无法获得其他答案。我认为这与 iOS 11 的自动将文本字段滚动到视图中的功能有关,尽管这尚未得到证实。

      我确实找到了其他几种方法来处理这个问题。

      Swift 4、iOS 11

      1。使用 UIView.animate

      UIView.animate(withDuration: 0.3, animations: {
          self.scrollView.contentInset = contentInsets
          self.scrollView.scrollIndicatorInsets = contentInsets
      }, completion: { (done) in
          var offset = self.scrollView.contentOffset
          offset.y = keyboardSize.height
          self.scrollView.setContentOffset(offset, animated: true)
      })
      

      2。使用安全区域(iOS 11 中的新功能)

      (注意:如果底部有标签栏或工具栏之类的现有插图,则可能效果不佳)

      additionalSafeAreaInsets.bottom = keyboardSize.height
      var offset = self.scrollView.contentOffset
      offset.y = keyboardSize.height
      self.scrollView.setContentOffset(offset, animated: true)
      

      3。在主线程上执行

      scrollView.contentInset = contentInsets
      scrollView.scrollIndicatorInsets = contentInsets
      
      DispatchQueue.main.async {
          var offset = self.scrollView.contentOffset
          offset.y = keyboardSize.height
          self.scrollView.setContentOffset(offset, animated: true)
      }
      

      【讨论】:

        【解决方案3】:

        在您的 keyboardDidShow 方法中添加类似的内容:

        var offset = scrollView.contentOffset
        offset.y = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.bounds.size.height
        scrollView.setContentOffset(offset, animated: true)
        

        【讨论】:

        • 我似乎无法在 iOS 11、Swift 4 中使用此功能。不确定自此答案以来是否发生了变化。即使我手动将值设置为offset.y,它也不会改变任何内容。仍然只是滚动显示文本字段而不滚动到底部。
        • 当您尝试滚动时,显示文本字段的动画仍在播放。做 DispatchQueue.main.async
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        相关资源
        最近更新 更多