ios 获取 UIScrollView 滚动到顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9450302/
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 17:02:58  来源:igfitidea点击:

Get UIScrollView to scroll to the top

iosobjective-ciphoneuiscrollviewuikit

提问by Hyman Humphries

How do I make a UIScrollView scroll to the top?

如何让 UIScrollView 滚动到顶部?

回答by rob mayoff

UPDATE FOR iOS 7

iOS 7 更新

[self.scrollView setContentOffset:
    CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];

ORIGINAL

原来的

[self.scrollView setContentOffset:CGPointZero animated:YES];

or if you want to preserve the horizontal scroll position and just reset the vertical position:

或者,如果您想保留水平滚动位置并重置垂直位置:

[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0)
    animated:YES];

回答by Andrew Schreiber

Here is a Swift extension that makes it easy:

这是一个 Swift 扩展,它使它变得容易:

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

Usage:

用法:

myScrollView.scrollToTop()

回答by Rajasekhar Pasupuleti

For Swift 4

对于 Swift 4

scrollView.setContentOffset(.zero, animated: true)

回答by Jakub Truhlá?

iOS 11 and above

iOS 11 及以上

try to work with the new adjustedContentInset.

尝试使用新的adjustedContentInset.

For example (scroll to the top):

例如(滚动到顶部):

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)

Even if you use prefersLargeTitles, safe areaetc.

即使你使用prefersLargeTitlessafe area等等。

回答by Bryan Chen

Use setContentOffset:animated:

setContentOffset:animated:

[scrollView setContentOffset:CGPointZero animated:YES];

回答by worthbak

Answer for Swift 2.0/3.0/4.0 and iOS 7+:

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)

回答by hoak

In iOS7 I had trouble getting a particular scrollview to go to the top, which worked in iOS6, and used this to set the scrollview to go to the top.

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

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

回答by Roni Leshes

Swift 3.0.1 version of rob mayoff's answer:

Swift 3.0.1 版本的rob mayoff 的回答

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

回答by user2898617

I think I have an answer that should be fully compatible with iOS 11 as well as prior versions (for vertical scrolling)

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

This takes into account the new adjustedContentInsetand also accounts for the additional offset required when prefersLargeTitlesis enabled on the navigationBar which appears to require an extra 52px offset on top of whatever the default is

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

This was a little tricky because the adjustedContentInset changes depending on the titleBar state (large title vs small title) so I needed to check and see what the titleBar height was and not apply the 52px offset if its already in the large state. Couldn't find any other method to check the state of the navigationBar so if anyone has a better option than seeing if the height is > 44.0 I'd like to hear it

这有点棘手,因为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)
    }
}

Inspired by Jakub's solution

灵感来自 Jakub 的解决方案

回答by Vitya Shurapov

It's very common when your navigation bar overlapsthe small portion of the scrollView content and it looks like content starts not from the top. For fixing it I did 2 things:

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

  • Size Inspector - Scroll View - Content Insets --> Change from Automatic to Never.
  • Size Inspector - Constraints- "Align Top to" (Top Alignment Constraints)- Second item --> Change from Superview.Top to Safe Area.Topand the value(constant field) set to 0
  • 尺寸检查器 - 滚动视图 -内容插入 --> 从自动更改为从不。
  • Size Inspector - Constraints- "Align Top to" (Top Alignment Constraints)- 第二项 -->从 Superview.Top 更改为 Safe Area.Top并将值(常量字段)设置为 0

Content insets - NeverAlign ScrolView.Top to Safe Area.Top

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