ios UIScrollView 以编程方式滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/952412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:59:10  来源:igfitidea点击:

UIScrollView scroll to bottom programmatically

iosobjective-cswiftuiscrollview

提问by nico

How can I make a UIScrollViewscroll to the bottom within my code? Or in a more generic way, to any point of a subview?

如何UIScrollView在我的代码中滚动到底部?或者以更通用的方式,到子视图的任何一点?

回答by Ben Gotow

You can use the UIScrollView's setContentOffset:animated:function to scroll to any part of the content view. Here's some code that would scroll to the bottom, assuming your scrollView is self.scrollView:

您可以使用 UIScrollView 的setContentOffset:animated:函数滚动到内容视图的任何部分。下面是一些可以滚动到底部的代码,假设您的 scrollView 是self.scrollView

CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];

Hope that helps!

希望有帮助!

回答by Esqarrouth

Swift version of the accepted answer for easy copy pasting:

已接受答案的 Swift 版本,便于复制粘贴:

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)

回答by Hai Hw

Simplest Solution:

最简单的解决方案:

[scrollview scrollRectToVisible:CGRectMake(scrollview.contentSize.width - 1,scrollview.contentSize.height - 1, 1, 1) animated:YES];

回答by vivek241

Just an enhancement to the existing answer.

只是对现有答案的增强。

CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];

It takes care of the bottom inset as well (in case you're using that to adjust your scroll view when the keyboard is visible)

它也会处理底部的插图(以防您在键盘可见时使用它来调整滚动视图)

回答by matt

Setting the content offset to the height of the content size is wrong: it scrolls the bottom of the content to the topof the scroll view, and thus out of sight.

将内容偏移设置为内容大小的高度是错误的:它将内容的底部滚动到滚动视图的顶部,从而看不见。

The correct solution is to scroll the bottom of the content to the bottomof the scroll view, like this (svis the UIScrollView):

正确的解决方案是将内容的底部滚动到滚动视图的底部,像这样(sv是 UIScrollView):

CGSize csz = sv.contentSize;
CGSize bsz = sv.bounds.size;
if (sv.contentOffset.y + bsz.height > csz.height) {
    [sv setContentOffset:CGPointMake(sv.contentOffset.x, 
                                     csz.height - bsz.height) 
                animated:YES];
}

回答by duan

A swifty implementation:

一个快速的实施:

extension UIScrollView {
   func scrollToBottom(animated: Bool) {
     if self.contentSize.height < self.bounds.size.height { return }
     let bottomOffset = CGPoint(x: 0, y: self.contentSize.height - self.bounds.size.height)
     self.setContentOffset(bottomOffset, animated: animated)
  }
}

use it:

用它:

yourScrollview.scrollToBottom(animated: true)

回答by onmyway133

A Swift 2.2 solution, taking contentInsetinto account

一个 Swift 2.2 解决方案,考虑contentInset

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)

This should be in an extension

这应该在扩展中

extension UIScrollView {

  func scrollToBottom() {
    let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
    setContentOffset(bottomOffset, animated: true)
  }
}

Note that you may want to check if bottomOffset.y > 0before scroll

请注意,您可能想bottomOffset.y > 0在滚动前检查是否

回答by Bart?omiej Semańczyk

What if contentSizeis lower than bounds?

如果contentSize低于bounds?

For Swift it is:

对于 Swift,它是:

scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0) ), animated: true)

回答by Tahir Waseem

Scroll To Top

滚动到顶部

- CGPoint topOffset = CGPointMake(0, 0);
- [scrollView setContentOffset:topOffset animated:YES];

Scroll To Bottom

滚动到底部

- CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
 - [scrollView setContentOffset:bottomOffset animated:YES];

回答by nico

I also found another useful way of doing this in the case you are using a UITableview (which is a subclass of UIScrollView):

在您使用 UITableview(它是 UIScrollView 的子类)的情况下,我还发现了另一种有用的方法:

[(UITableView *)self.view scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];