ChatGPT解决这个技术问题 Extra ChatGPT

Get UIScrollView to scroll to the top

How do I make a UIScrollView scroll to the top?


r
rob mayoff

UPDATE FOR 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];

If you're doing this for iOS 7, you may have to take into account the UIScrollView contentInset, unfortunately. [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, -self.scrollView.contentInset.top) animated:YES]; does the job for me
I was able to scroll to the top, right below the navigation and status bar. [scrollView setContentOffset:CGPointMake(0, -scrollView.contentInset.top) animated:YES]; It works in iOS 6 & 7
This didn't work for me, probably because I'm doing something crazy with an inverted view in sprite kit. But if anyone else is as crazy as me - and your scrollview is at the bottom by default - this will scroll it to the top: [scrollView setContentOffset:CGPointMake(0, scrollView.contentSize.height-scrollView.frame.size.height) animated:YES];
Late-in-the-day comment: you should adjust for the content inset regardless of OS version. iOS 7 happens to use it for allowing scroll content below the status bar but it's always been exposed as a property, so always been potentially used by somebody.
For iOS 11+, Jakub Truhlář's answer below using adjustedContentInset was helpful, at least in my case.
A
Andrew Schreiber

Here is a Swift extension that makes it easy:

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

Usage:

myScrollView.scrollToTop()

M
Massimo Polimeni

For Swift 4

scrollView.setContentOffset(.zero, animated: true)

Definitely the best modern answer.
J
Jakub Truhlář

iOS 11 and above

Try to play around with the new adjustedContentInset (It should even work with prefersLargeTitles, safe area etc.)

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)

J
Jack Humphries

Use setContentOffset:animated:

[scrollView setContentOffset:CGPointZero animated:YES];

C
Cœur

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

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

C
Cœur

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.

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

I have the same issue in iOS7. Does anyone know how to enable this for a UITableView?
It's funny how this works when animated is NO, but not when animated is YES. If YES, then it scrolls almost to the topdevgm16
A
Abdul Karim Khan

In SWIFT 5 Just set content Offset to zero

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

C
Community

Swift 3.0.1 version of rob mayoff's answer :

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

u
user2898617

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

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

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

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


V
Vitya Shurapov

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

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.Top and the value(constant field) set to 0

https://i.stack.imgur.com/XpLaE.png


O
Ortwin Gentz

To fully replicate the status bar scrollToTop behavior we not only have to set the contentOffset but also want to make sure the scrollIndicators are displayed. Otherwise the user can quickly get lost.

The only public method to accomplish this is flashScrollIndicators. Unfortunately, calling it once after setting the contentOffset has no effect because it's reset immediately. I found it works when doing the flash each time in 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;
    });
}

In your UIScrollViewDelegate (or UITable/UICollectionViewDelegate) implement this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.tag == SCROLLVIEW_IS_SCROLLING_TO_TOP_TAG) {
        [scrollView flashScrollIndicators];
    }
}

The hide delay is a bit shorter compared to the status bar scrollToTop behavior but it still looks nice.

Note that I'm abusing the view tag to communicate the "isScrollingToTop" state because I need this across view controllers. If you're using tags for something else you might want to replace this with an iVar or a property.


I haven't tested, but it should throw this on the next runloop if you just use DISPATCH_TIME_NOW instead. Thanks for doing this work, not sure I need it yet, but good to know.
It's a while ago and I don't remember exactly but I believe this didn't work. Deferring to the next runloop is usually the first thing I try in such cases.
p
pkamb

In modern iOS, set the the scroll view's content offset back to its top left adjustedContentInset:

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

S
Shoaib

Scroll to top for UITableViewController, UICollectionViewController or any UIViewController having UIScrollView

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)
            }
        }
    }
  }
}

b
bhucho

iOS 2.0+ Mac Catalyst 13.0+

You can try: scrollView.scrollsToTop = true

You can refer it from documentation of developer.apple.com


Hi and welcome to Stack Overflow! Please take the tour. Thanks for contributing an answer but can you also add an explanation on how it solves the problem?
Doing this will only enable/disable the scroll-to-top gesture, which is not what the question is about.
N
Narasimha Nallamsetty

I tried all the ways. But nothing worked for me. Finally I did like this.

I added self.view .addSubview(self.scroll) line of code in the viewDidLoad. After started setting up frame for scroll view and added components to scroll view.

It worked for me.

Make sure you added self.view .addSubview(self.scroll) line in the beginning. then you can add UI elements.


Please delete this. While it surely made sense at the time, your subview hierarchy issues aren't relevant to this question.
@Cristik if your scroll view isn't in the view hierarchy, or your computer is switched off, of course none of this will work. But the OP asked something pretty narrow