418

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section number 5.

So I get an exception, when I call:

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

Is there another way to scroll to the top of table view?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168

37 Answers37

906

UITableView is a subclass of UIScrollView, so you can also use:

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

Or

[mainTableView setContentOffset:CGPointZero animated:YES];

And in Swift:

mainTableView.setContentOffset(CGPointZero, animated:true)

And in Swift 3 & above:

mainTableView.setContentOffset(.zero, animated: true)
catlan
  • 25,100
  • 8
  • 67
  • 78
  • Worked for me too, even with a tableHeaderView – ıɾuǝʞ Dec 09 '11 at 09:16
  • thanks. I just added that to my 'scrollToRowAtIndexPath' line and scrolling works fine now. – Nasenbaer Apr 17 '12 at 19:44
  • 11
    I first tried this with CGRectZero, which is equivalent to CGRectMake(0, 0, 0, 0). This does not work, but oddly the above does. I guess it needs a positive width and height. Thank you. – Keller Jan 14 '13 at 17:57
  • Using this code to go at some uiview in scroll: [scrollView scrollRectToVisible:CGRectMake(0, yourview.frame.origin.y, 1, 1) animated:YES]; – Ele Nov 13 '13 at 15:01
  • 5
    Note, you will want **animated:NO** if you run this in scrollToRowAtIndexPath: so as to make the table start in the correct position. Hope it helps! – Fattie Jan 27 '14 at 14:25
  • 4
    @hasan83 CGRectMake(0, 0, 0, 0) or CGRectZero isn't a visible rect. I would also say that [mainTableView setContentOffset:CGPointZero animated:YES]; is the prettier expression. – catlan Apr 27 '15 at 05:01
  • I tried this solution in xcode 6 + swift. not really working. any possible reason where I am wrong or why this is not working any more? – Junchao Gu Jun 16 '15 at 06:53
  • @JunchaoGu some code example would help. Should work in swift. – catlan Jun 16 '15 at 19:46
  • the code sample is too long to post here but basically I am calling a function from viewWillAppear, if some condition is met, I will be scrolling the table view to the top and insert some rows with UIView.animation. The solution from fabb worked but this one did not really scroll – Junchao Gu Jun 17 '15 at 01:47
  • @JunchaoGu can only suggest to create a stackexchange question with some of the code and maybe a video showing the scroll problem... – catlan Jun 17 '15 at 21:16
  • `mainTableView.setContentOffset(CGPoint.zero, animated:true)` is more "swifty". – Tim Vermeulen Feb 07 '16 at 23:41
  • This didn't work for me in a table with multiple sections, each with a header. I had to use tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false) – Leon Jul 04 '17 at 14:15
  • This worked fine for me (Swift 3, XCode 8), but only after I remembered to call `endRefreshing()` before since my UITableView contains an UIRefreshControl. – idrougge Sep 08 '17 at 11:22
  • 8
    anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases? – Peter Lapisu May 10 '18 at 10:41
  • 17
    @PeterLapisu `self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true)` seems to work in iOS 11 – Jordan Soltman Aug 06 '18 at 07:18
  • 1
    nope, i tested this too, and all other methods to scroll – Peter Lapisu Aug 06 '18 at 17:38
  • @PeterLapisu I'm getting the same problem, after switching ViewController via TabBar every answer in this page stop working, did you manage to resolve the issue? – Carioni Feb 27 '19 at 16:43
  • @PeterLapisu For iOS 11 or later, see https://stackoverflow.com/a/48315689/1226963 – rmaddy Aug 03 '19 at 05:29
  • the answer is correct but it may not work sometimes unless you enclose that code inside tableView.beginUpdates() and tableView.endUpdates(). After this my scroll always works. – prodev Nov 20 '20 at 07:28
250

Note: This answer isn't valid for iOS 11 and later.

I prefer

[mainTableView setContentOffset:CGPointZero animated:YES];

If you have a top inset on your table view, you have to subtract it:

[mainTableView setContentOffset:CGPointMake(0.0f, -mainTableView.contentInset.top) animated:YES];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
fabb
  • 11,660
  • 13
  • 67
  • 111
  • 12
    Tomato, tomato. Hmm... That doesn't come across very clearly in writing. – FreeAsInBeer Apr 01 '12 at 02:29
  • 14
    This is the best way to use when you have table header or footer views and want them to be included too. – mafonya Aug 02 '12 at 20:52
  • 7
    This is indeed a clear code, but it does not work when your `tableView` has non-zero `contentInset` from the top. For example: `tableView.contentInset = UIEdgeInsetsMake(5.0f, 0.0f, 250.0f, 0.0f);`. If that is the case, in your code the `tableView` scrolls to `(0.0f, 5.0f)`. – tolgamorf May 08 '13 at 20:36
  • 26
    The solution to my previous comment: `[tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];` – tolgamorf May 08 '13 at 20:40
  • This is what I tried first. Strangely it didn't work, which is what sent me here. `scrollRectToVisible` does work. – bugloaf Nov 16 '13 at 23:56
  • tried catlan's answer but that one did not work. this answer worked well :P – Junchao Gu Jun 16 '15 at 06:54
  • 3
    On iOS 11 you should consider using `-scrollView.adjustedContentInset.top` instead. – Marián Černý Mar 04 '18 at 11:54
89

Possible Actions:

1

func scrollToFirstRow() {
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}

2

func scrollToLastRow() {
    let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}

3

func scrollToSelectedRow() {
    let selectedRows = self.tableView.indexPathsForSelectedRows
    if let selectedRow = selectedRows?[0] as? NSIndexPath {
        self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
    }
}

4

func scrollToHeader() {
    self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}

5

func scrollToTop(){
    self.tableView.setContentOffset(CGPointMake(0,  UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
}

Disable Scroll To Top:

func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
    for subview in view.subviews {
        if let scrollView = subview as? UIScrollView {
            (scrollView as UIScrollView).scrollsToTop = false
        }
        self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
    }
}

Modify and use it as per requirement.

Swift 4

  func scrollToFirstRow() {
    let indexPath = IndexPath(row: 0, section: 0)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
  }
Adrian
  • 16,233
  • 18
  • 112
  • 180
Alvin George
  • 14,148
  • 92
  • 64
27

It's better to not use NSIndexPath (empty table), nor assume that top point is CGPointZero (content insets), that's what I use -

[tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];

Hope this helps.

Kof
  • 23,893
  • 9
  • 56
  • 81
23

Swift 4:

This works very well:

//self.tableView.reloadData() if you want to use this line remember to put it before 
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • 3
    Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content) – finneycanhelp Mar 12 '18 at 19:48
  • 1
    I have both a plain and a `UITableViewStyleGrouped` (headers scrolls with the contents) and this code works. Be sure you are in the main thread and you launch this code after the view appear (`viewDidAppear`). If you still have problems try to put the code inside this: `DispatchQueue.main.asyncAfter(deadline: .now()+0.1, execute: { // the code }` – Alessandro Ornano Mar 12 '18 at 20:55
  • 1
    Thank you. It will get you to the first cell. However, it doesn't show the table view header. – finneycanhelp Mar 14 '18 at 01:13
  • This will fail if the tableView is empty for some reason (no cell in 0 section at 0 row) – Maxim Skryabin Jan 24 '19 at 08:49
22

DONT USE

 tableView.setContentOffset(.zero, animated: true)

It can sometimes set the offset improperly. For example, in my case, the cell was actually slightly above the view with safe area insets. Not good.

INSTEAD USE

 tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
  • 1
    Perfect Solution. – Baby Groot Jun 11 '19 at 12:50
  • Neither of these solutions are valid. The first doesn't work under iOS 11 or later. The second doesn't work if the table view has a table header or the first section has a section header unless you really do want the first row at the top and don't care about showing any headers. – rmaddy Aug 03 '19 at 05:28
  • @rmaddy what's the best solution? – ScottyBlades May 28 '20 at 23:47
21

On iOS 11, use adjustedContentInset to correctly scroll to top for both cases when the in-call status bar is visible or not.

if (@available(iOS 11.0, *)) {
    [tableView setContentOffset:CGPointMake(0, -tableView.adjustedContentInset.top) animated:YES];
} else {
    [tableView setContentOffset:CGPointMake(0, -tableView.contentInset.top) animated:YES];
}

Swift:

if #available(iOS 11.0, *) {
    tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
} else {
    tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
}
Thanh Pham
  • 2,021
  • 21
  • 30
  • It may not work sometimes unless you enclose that code inside tableView.beginUpdates() and tableView.endUpdates(). After this my scroll always works. – prodev Nov 20 '20 at 07:31
19

I've encountered an issue calling trying some of the methods on an empty tableView. Here's another option for Swift 4 that handles empty tableviews.

extension UITableView {
  func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
    return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
  }

  func scrollToTop(animated: Bool) {
    let indexPath = IndexPath(row: 0, section: 0)
    if self.hasRowAtIndexPath(indexPath: indexPath) {
      self.scrollToRow(at: indexPath, at: .top, animated: animated)
    }
  }
}

Usage:

// from yourViewController or yourTableViewController
tableView.scrollToTop(animated: true)//or false
MobileMon
  • 8,341
  • 5
  • 56
  • 75
Adrian
  • 16,233
  • 18
  • 112
  • 180
12

For tables that have a contentInset, setting the content offset to CGPointZero will not work. It'll scroll to the content top vs. scrolling to the table top.

Taking content inset into account produces this instead:

[tableView setContentOffset:CGPointMake(0, -tableView.contentInset.top) animated:NO];
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Eran Goldin
  • 980
  • 12
  • 21
10

This code let's you scroll a specific section to top

CGRect cellRect = [tableinstance rectForSection:section];
CGPoint origin = [tableinstacne convertPoint:cellRect.origin 
                                    fromView:<tableistance>];
[tableinstance setContentOffset:CGPointMake(0, origin.y)];
dreamzor
  • 5,795
  • 4
  • 41
  • 61
Ramesh Muthe
  • 811
  • 7
  • 15
10

Swift 5, iOS 13

I know this question already has a lot of answers but from my experience this method always works:

let last = IndexPath(row: someArray.count - 1, section: 0)
tableView.scrollToRow(at: last, at: .bottom, animated: true)

And this is especially true if you're working with animations (like keyboard) or certain async tasks—the other answers will often scroll to the almost bottom. If for some reason this doesn't get you all the way to the bottom, it's almost certainly because of a competing animation so the workaround is to dispatch this animation to the end of the main queue:

DispatchQueue.main.async {
    let last = IndexPath(row: self.someArray.count - 1, section: 0)
    self.tableView.scrollToRow(at: last, at: .bottom, animated: true)
}

This may seem redundant since you're already on the main queue but it's not because it serializes the animations.

trndjc
  • 11,654
  • 3
  • 38
  • 51
  • You also need to check if there is a section – Onur Tuna Feb 02 '20 at 13:11
  • This works as long as you don't have zero rows in your tableView. Otherwise, I'd recommend using @catlan's answer – craft Dec 09 '20 at 18:17
  • @craft if you have zero rows in your table then you shouldn't be attempting to scroll to the bottom anyway because it's already there. – trndjc Dec 09 '20 at 18:34
  • @bsod Thanks for the reply. I actually do have a case where it's relevant. I'm scrolling back to the top with section headers but no rows. Also, I think the question is about `top` not `bottom` – craft Dec 09 '20 at 18:45
8

Swift:

tableView.setContentOffset(CGPointZero, animated: true)
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
8

Swift 3

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

if tableView.setContentOffset don't work.

Use:

tableView.beginUpdates()
tableView.setContentOffset(CGPoint.zero, animated: true)
tableView.endUpdates()
7

Adding on to what's already been said, you can create a extension (Swift) or category (Objective C) to make this easier in the future:

Swift:

extension UITableView {
    func scrollToTop(animated: Bool) {
        setContentOffset(CGPointZero, animated: animated)
    }
}

Any time you want to scroll any given tableView to the top you can call the following code:

tableView.scrollToTop(animated: true)
ocwang
  • 664
  • 5
  • 9
7

Since my tableView is full of all kinds of insets, this was the only thing that worked well:

Swift 3

if tableView.numberOfSections > 0 && tableView.numberOfRows(inSection: 0) > 0 {
  tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}

Swift 2

if tableView.numberOfSections > 0 && tableView.numberOfRowsInSection(0) > 0 {
  tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
6

I prefer the following, as it takes into account an inset. If there is no inset, it will still scroll to the top as the inset will be 0.

tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
jacob
  • 3,507
  • 2
  • 21
  • 26
5

Swift :

if you don't have tableView header :

tableView.setContentOffset(CGPointMake(0,  UIApplication.sharedApplication().statusBarFrame.height ), animated: true)

if so :

tableView.setContentOffset(CGPointMake(0, -tableViewheader.frame.height   + UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
jmcastel
  • 1,365
  • 7
  • 17
  • 34
5

In Swift 5 , Thanks @Adrian's answer a lot

extension UITableView{

    func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
        return indexPath.section < numberOfSections && indexPath.row < numberOfRows(inSection: indexPath.section)
    }

    func scrollToTop(_ animated: Bool = false) {
        let indexPath = IndexPath(row: 0, section: 0)
        if hasRowAtIndexPath(indexPath: indexPath) {
            scrollToRow(at: indexPath, at: .top, animated: animated)
        }
    }

}

Usage:

tableView.scrollToTop()
dengST30
  • 3,643
  • 24
  • 25
  • 3
    1. This is also valid in Swift 4. 2. This doesn't work if there is a table header or a section header. – rmaddy Aug 03 '19 at 05:39
4

Here's what I use to work correctly on iOS 11:

extension UIScrollView {
    func scrollToTop(animated: Bool) {
        var offset = contentOffset
        if #available(iOS 11, *) {
            offset.y = -adjustedContentInset.top
        } else {
            offset.y = -contentInset.top
        }
        setContentOffset(offset, animated: animated)
    }
}
Hans Brende
  • 7,847
  • 4
  • 37
  • 44
3

using contentOffset is not the right way. this would be better as it is table view's natural way

tableView.scrollToRow(at: NSIndexPath.init(row: 0, section: 0) as IndexPath, at: .top, animated: true)
Gulz
  • 1,773
  • 19
  • 15
3

This was the only code snippet that worked for me

Swift 4:

    tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    tableView.setContentOffset(CGPoint(x: 0, y: -70), animated: true)

P.S. 70 is the height of my header and table view cell

jimmyruby
  • 73
  • 9
  • This is far from the correct way to scroll to the top. It makes no sense to use three separate lines just to set the content offset. Only the last line is needed but hardcoding a specific offset is not the correct solution. – rmaddy Aug 03 '19 at 05:41
2
func scrollToTop() {
        NSIndexPath *topItem = [NSIndexPath indexPathForItem:0 inSection:0];
        [tableView scrollToRowAtIndexPath:topItem atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

call this function wherever you want UITableView scroll to top

2

Swift 4 via extension, handles empty table view:

extension UITableView {
    func scrollToTop(animated: Bool) {
        self.setContentOffset(CGPoint.zero, animated: animated);
    }
}
Shawn
  • 311
  • 2
  • 9
2

I use tabBarController and i have a few section in my tableview at every tab, so this is best solution for me.

extension UITableView {

    func scrollToTop(){

        for index in 0...numberOfSections - 1 {
            if numberOfSections > 0 && numberOfRows(inSection: index) > 0 {
                scrollToRow(at: IndexPath(row: 0, section: index), at: .top, animated: true)
                break
            }

            if index == numberOfSections - 1 {
                setContentOffset(.zero, animated: true)
                break
            }
        }

    }

}
Okan
  • 410
  • 4
  • 10
1

in swift

your row = selectioncellRowNumber your section if you have = selectionNumber if you dont have set is to zero

//UITableViewScrollPosition.Middle or Bottom or Top

var lastIndex = NSIndexPath(forRow:  selectioncellRowNumber, inSection: selectionNumber)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
idris yıldız
  • 2,097
  • 20
  • 22
1

I had to add the multiply by -1 * to the sum of the status bar and the navigation bar, because it was going that height off the screen,

self.tableView.setContentOffset(CGPointMake(0 , -1 * 
  (self.navigationController!.navigationBar.height +  
  UIApplication.sharedApplication().statusBarFrame.height) ), animated:true)
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Carlos.V
  • 409
  • 6
  • 13
1

Here Is The Code To ScrollTableView To Top Programatically

Swift:

self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)
Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47
1

In Swift-3 :

self.tableView.setContentOffset(CGPoint.zero, animated: true)
Jeni Khant
  • 350
  • 3
  • 13
1

If you need use Objective-C or you still in love:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
handzel
  • 91
  • 1
  • 10
1

Solution with scrollToRow which fixes the problem for empty TableView (needs for search).

import UIKit

extension UITableView {
    
    public func scrollToTop(animated: Bool = false) {
        if numberOfRows(inSection: 0) > 0 {
            scrollToRow(
                at: .init(row: 0, section: 0),
                at: .top,
                animated: animated
            )
        }
    }
    
}
user2168735
  • 395
  • 4
  • 14
1

The easiest way that I found to force UITableViewController to scroll-to-the-top under iPhone, iPad and macCatalyst (macOS) is as follows:

  • prepare to return 0 from [tableView:numberOfRowsInSection:]
  • call [self.tableView reloadData]
  • call [self.tableView layoutIfNeeded]
  • prepare to return the factual row count from [tableView:numberOfRowsInSection:]
  • call [self.tableView reloadData]
Gary
  • 815
  • 10
  • 16
0

If you i would like move scroll animation in the table, use this code. The scroll move to top with animation in .5 seconds.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

[_tableContent scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

[UIView commitAnimations];
Matheus Veloza
  • 492
  • 5
  • 4
0

To have a completion when finished, add this extension

// MARK: - UIScrollView

extension UIScrollView {

    /// Animate scroll to top with completion
    ///
    /// - Parameters:
    ///   - duration:   TimeInterval
    ///   - completion: Completion block
    func animateScrollToTop(withDuration duration:  TimeInterval,
                            completion:             @escaping (()->())) {

        UIView.animate(withDuration: duration, animations: { [weak self] in
                self?.setContentOffset(CGPoint.zero, animated: false)
            }, completion: { finish in
                guard finish else {
                    return
                }
                completion()
        })
    }
}

tableView.animateScrollToTop(withDuration: 0.25) { 
    // Finish
}
YanSte
  • 10,661
  • 3
  • 57
  • 53
0

Here is a simple example of UIScrollView extension that you can use everywhere in your app.

1) First you should create enum with possible scroll directions:

enum ScrollDirection {
    case top, right, bottom, left

    func contentOffsetWith(_ scrollView: UIScrollView) -> CGPoint {
        var contentOffset = CGPoint.zero
        switch self {
            case .top:
                contentOffset = CGPoint(x: 0, y: -scrollView.contentInset.top)
            case .right:
                contentOffset = CGPoint(x: scrollView.contentSize.width - scrollView.bounds.size.width, y: 0)
            case .bottom:
                contentOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
            case .left:
                contentOffset = CGPoint(x: -scrollView.contentInset.left, y: 0)
            }
        return contentOffset
    }
}

2) Then add extension to UIScrollView:

extension UIScrollView {
    func scrollTo(direction: ScrollDirection, animated: Bool = true) {
        self.setContentOffset(direction.contentOffsetWith(self), animated: animated)
    }
}

3) Thats it! Now you can use it:

myScrollView.scrollTo(.top, animated: false)

This scroll bind to the tableView's content size and it looks more natural than scroll to CGPoint.zero

Raman Shyniauski
  • 392
  • 2
  • 13
-1

With Swift:

self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
Dhananjay Patil
  • 442
  • 3
  • 10
-3

Use this code for UITableview implementation in swift:

var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
if cell == nil {
    cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
}
anacron
  • 6,443
  • 2
  • 26
  • 31
Pradnya
  • 7
  • 1
  • 4
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – J. Chomel Feb 21 '17 at 07:05
  • The question was how to scroll UITableView to top. This post does not answer the question. – Pang Feb 24 '17 at 06:52
-31

If you don't want scrolling, you can start and stop the scrolling animation as soon as you start it.

    $('document').ready(function() {
    $("html, body").animate({ scrollTop: 0 }, 500);
    return true;
    });

Also, to animate put values for 'x' and 'y', passing in 0,0 will scroll the page to the top left instantly.

window.scrollTo(x, y);