繁体   English   中英

获取 UIScrollView 滚动到顶部

[英]Get UIScrollView to scroll to the top

如何让 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 扩展,它使它变得容易:

extension UIScrollView {
    func scrollToTop() {
        let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
        setContentOffset(desiredOffset, animated: true)
   }
}

用法:

myScrollView.scrollToTop()

对于 Swift 4

scrollView.setContentOffset(.zero, animated: true)

iOS 11 及以上

试着玩的新adjustedContentInset (它甚至应该与工作prefersLargeTitlessafe area等)

例如(滚动到顶部):

var offset = CGPoint(
    x: -scrollView.contentInset.left, 
    y: -scrollView.contentInset.top)

if #available(iOS 11.0, *) {
    offset = CGPoint(
        x: -scrollView.adjustedContentInset.left, 
        y: -scrollView.adjustedContentInset.top)    
}

scrollView.setContentOffset(offset, animated: true)

使用setContentOffset:animated:

[scrollView setContentOffset:CGPointZero animated:YES];

Swift 2.0/3.0/4.0 和 iOS 7+ 的答案:

let desiredOffset = CGPoint(x: 0, y: -self.scrollView.contentInset.top)
self.scrollView.setContentOffset(desiredOffset, animated: true)

在 iOS7 中,我无法让特定的滚动视图转到顶部,这在 iOS6 中有效,并使用它来设置滚动视图转到顶部。

[self.myScroller scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

Swift 3.0.1 版本的rob mayoff 的回答

self.scrollView.setContentOffset(
CGPoint(x: 0,y: -self.scrollView.contentInset.top),
animated: true)

我想我的答案应该与 iOS 11 以及之前的版本完全兼容(用于垂直滚动)

这考虑了新的adjustedContentInset并考虑了在导航上启用prefersLargeTitles时所需的额外偏移量,这似乎需要在默认值之上额外的52px 偏移量

这有点棘手,因为adjustedContentInset 根据titleBar 状态(大标题与小标题)而变化,所以我需要检查并查看titleBar 高度是多少,如果它已经处于大状态,则不应用52px 偏移量。 找不到任何其他方法来检查导航栏的状态,所以如果有人有比查看高度是否 > 44.0 更好的选择,我想听听

func scrollToTop(_ scrollView: UIScrollView, animated: Bool = true) {
    if #available(iOS 11.0, *) {
        let expandedBar = (navigationController?.navigationBar.frame.height ?? 64.0 > 44.0)
        let largeTitles = (navigationController?.navigationBar.prefersLargeTitles) ?? false
        let offset: CGFloat = (largeTitles && !expandedBar) ? 52: 0
        scrollView.setContentOffset(CGPoint(x: 0, y: -(scrollView.adjustedContentInset.top + offset)), animated: animated)
    } else {
        scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.contentInset.top), animated: animated)
    }
}

灵感来自 Jakub 的解决方案

在 SWIFT 5 中只需将内容偏移设置为零

scrollView.setContentOffset(CGPoint.zero, animated: true)

要完全复制状态栏 scrollToTop 行为,我们不仅要设置 contentOffset,还要确保显示 scrollIndicators。 否则用户很快就会迷路。

完成此操作的唯一公共方法是flashScrollIndicators 不幸的是,在设置 contentOffset 后调用一次它没有效果,因为它会立即重置。 我发现每次在scrollViewDidScroll:执行 flash 时它都有效。

// 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”状态,因为我需要跨视图控制器使用它。 如果您将标签用于其他用途,您可能希望将其替换为 iVar 或属性。

您的导航栏与scrollView 内容的一小部分重叠并且看起来内容不是从顶部开始时,这是很常见的。 为了修复它,我做了两件事:

  • 尺寸检查器 - 滚动视图 -内容插入 --> 从自动更改为从不。
  • Size Inspector - Constraints- "Align Top to" (Top Alignment Constraints)- 第二项 -->从 Superview.Top 更改为 Safe Area.Top并将值(常量字段)设置为 0

内容插入 - 从不 将ScrollView.Top 与Safe Area.Top 对齐

滚动到UITableViewControllerUICollectionViewController或任何具有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)
            }
        }
    }
  }
}

在现代 iOS 中,将滚动视图的内容偏移设置回其左上角adjustedContentInset

let point = CGPoint(x: -scrollView.adjustedContentInset.left,
                    y: -scrollView.adjustedContentInset.top)
    
scrollView.setContentOffset(point, animated: true)

iOS 2.0+ Mac Catalyst 13.0+

你可以试试: scrollView.scrollsToTop = true

您可以从developer.apple.com 的文档中参考它

我尝试了所有方法。 但没有什么对我有用。 最后我是这样的。

我在 viewDidLoad 中添加了self.view .addSubview(self.scroll)代码行。 开始为滚动视图设置框架并为滚动视图添加组件后。

它对我有用。

确保在开头添加了self.view .addSubview(self.scroll)行。 然后你可以添加 UI 元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM