获取UIScrollView滚动到顶部
如何设置UIScrollView滚动到顶部?
更新iOS 7
[self.scrollView setContentOffset: CGPointMake(0, -self.scrollView.contentInset.top) animated:YES]; 原版的
 [self.scrollView setContentOffset:CGPointZero animated:YES]; 
或者如果你想保持水平滚动位置,只需重置垂直位置:
 [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0) animated:YES]; 
这是一个Swift 2&3扩展,可以很容易:
 extension UIScrollView { func scrollToTop() { let desiredOffset = CGPoint(x: 0, y: -contentInset.top) setContentOffset(desiredOffset, animated: true) } } 
用法:
 myScrollView.scrollToTop() 
 使用setContentOffset:animated: 
 [scrollView setContentOffset:CGPointZero animated:YES]; 
回答Swift 2.0和iOS 7+:
 let desiredOffset = CGPoint(x: 0, y: -self.scrollView.contentInset.top) self.scrollView.setContentOffset(desiredOffset, animated: true) 
更新10/6/16:看起来像这个在Swift 3.0中仍然有效
在iOS7中,我很难得到一个特定的滚动视图到顶部,这在iOS6中工作,并使用此设置滚动视图到顶部。
 [self.myScroller scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 
 对于iOS 11及以上版本 – 使用新的adjustedContentInset 
例如:
 if #available(iOS 11.0, *) { let offset = CGPoint(x: -scrollView.adjustedContentInset.left, y: -scrollView.adjustedContentInset.top) scrollView.setContentOffset(offset, animated: true) } else { let offset = CGPoint(x: -scrollView.contentInset.left, y: -scrollView.contentInset.top) scrollView.setContentOffset(offset, animated: true) } 
 即使你使用prefersLargeTitles , Safe Area等 
Swift 3.0.1版抢劫mayoff的回答 :
 self.scrollView.setContentOffset( CGPoint(x: 0,y: -self.scrollView.contentInset.top), animated: true) 
要完全复制状态栏的scrollToTop行为,我们不仅要设置contentOffset,还要确保显示scrollIndicators。 否则用户可能会很快迷路。
 唯一的公共方法是flashScrollIndicators 。 不幸的是,在设置contentOffset后调用它一次没有效果,因为它立即被重置。 我发现它在每次在scrollViewDidScroll:执行flash时都scrollViewDidScroll: 。 
 // define arbitrary tag number in a global constants or in the .pch file #define SCROLLVIEW_IS_SCROLLING_TO_TOP_TAG 19291 - (void)scrollContentToTop { [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, -self.scrollView.contentInset.top) animated:YES]; self.scrollView.tag = SCROLLVIEW_IS_SCROLLING_TO_TOP_TAG; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.scrollView.tag = 0; }); } 
在你的UIScrollViewDelegate(或UITable / UICollectionViewDelegate)实现这个:
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == SCROLLVIEW_IS_SCROLLING_TO_TOP_TAG) { [scrollView flashScrollIndicators]; } } 
与状态栏scrollToTop行为相比,隐藏延迟有点短,但它看起来还是不错的。
请注意,我滥用视图标记来传达“isScrollingToTop”状态,因为我需要跨视图控制器。 如果您使用标签作为其他东西,则可能需要将其replace为iVar或属性。
 滚动到顶部的UITableViewController , UICollectionViewController或任何具有UIScrollView UIViewController 
 extension UIViewController { func scrollToTop(animated: Bool) { if let tv = self as? UITableViewController { tv.tableView.setContentOffset(CGPoint.zero, animated: animated) } else if let cv = self as? UICollectionViewController{ cv.collectionView?.setContentOffset(CGPoint.zero, animated: animated) } else { for v in view.subviews { if let sv = v as? UIScrollView { sv.setContentOffset(CGPoint.zero, animated: animated) } } } } } 
我尝试了所有的方法。 但是没有为我工作。 最后我确实喜欢这个。
 我在viewDidLoad中添加了self.view .addSubview(self.scroll)代码行。 开始为滚动视图设置框架并添加组件以滚动视图。 
它为我工作。
 确保你在开头添加了self.view .addSubview(self.scroll)行。 那么你可以添加UI元素。