UITableView - scroll to the top











up vote
343
down vote

favorite
81












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?










share|improve this question




























    up vote
    343
    down vote

    favorite
    81












    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?










    share|improve this question


























      up vote
      343
      down vote

      favorite
      81









      up vote
      343
      down vote

      favorite
      81






      81





      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?










      share|improve this question















      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?







      ios objective-c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 6 '17 at 4:56









      Leo Dabus

      127k30259335




      127k30259335










      asked Apr 7 '09 at 9:54









      Ilya Suzdalnitski

      27.3k44118163




      27.3k44118163
























          31 Answers
          31






          active

          oldest

          votes













          1 2
          next











          up vote
          772
          down vote



          accepted










          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)





          share|improve this answer



















          • 9




            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






          • 4




            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






          • 3




            @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 5:01






          • 4




            anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
            – Peter Lapisu
            May 10 at 10:41






          • 2




            @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
            – Jordan S
            Aug 6 at 7:18


















          up vote
          236
          down vote













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





          share|improve this answer



















          • 12




            Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
            – FreeAsInBeer
            Apr 1 '12 at 2: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 2 '12 at 20:52






          • 4




            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 8 '13 at 20:36






          • 24




            The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
            – tolgamorf
            May 8 '13 at 20:40






          • 1




            It works but doesn't take the 64px offset in regard
            – Departamento B
            May 20 '15 at 16:11


















          up vote
          54
          down vote













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





          share|improve this answer



















          • 5




            Good solutions, scrollToFirstRow() works for me
            – Kyle KIM
            Mar 30 '16 at 12:46










          • Thats the solution, perfect - scrollToFirstRow
            – Mehul
            Nov 29 '16 at 6:28












          • Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
            – Vincent
            Jan 30 '17 at 8:40


















          up vote
          26
          down vote













          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.






          share|improve this answer




























            up vote
            15
            down vote













            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)





            share|improve this answer

















            • 1




              Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
              – finneycanhelp
              Mar 12 at 19:48












            • 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 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 at 1:13


















            up vote
            12
            down vote













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





            share|improve this answer



















            • 1




              Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
              – johnrechd
              Feb 6 at 17:00


















            up vote
            10
            down vote













            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)];





            share|improve this answer























            • This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
              – Felipe
              Aug 31 '14 at 1:56










            • This code is very useful to make the section scroll top
              – srinivas n
              Sep 18 '14 at 12:01


















            up vote
            9
            down vote













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





            share|improve this answer




























              up vote
              8
              down vote













              Swift:



              tableView.setContentOffset(CGPointZero, animated: true)





              share|improve this answer




























                up vote
                8
                down vote













                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)





                share|improve this answer






























                  up vote
                  7
                  down vote













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





                  share|improve this answer




























                    up vote
                    6
                    down vote













                    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)





                    share|improve this answer




























                      up vote
                      5
                      down vote













                      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() {
                      let indexPath = IndexPath(row: 0, section: 0)
                      if self.hasRowAtIndexPath(indexPath: indexPath) {
                      self.scrollToRow(at: indexPath, at: .top, animated: true)
                      }
                      }
                      }


                      Usage:



                      // from yourViewController or yourTableViewController
                      tableView.scrollToTop()





                      share|improve this answer






























                        up vote
                        4
                        down vote













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





                        share|improve this answer





















                        • this was more useful to me
                          – Tejzeratul
                          Aug 10 '17 at 22:06










                        • great answer, appreciate it
                          – Jeremy Bader
                          Aug 14 '17 at 1:45


















                        up vote
                        4
                        down vote













                        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)





                        share|improve this answer





















                        • This was the part I was missing.
                          – That Guy
                          Jul 20 '17 at 12:50


















                        up vote
                        2
                        down vote













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





                        share|improve this answer




























                          up vote
                          2
                          down vote













                          Swift 4 via extension, handles empty table view:



                          extension UITableView {
                          func scrollToTop(animated: Bool) {
                          self.setContentOffset(CGPoint.zero, animated: animated);
                          }
                          }





                          share|improve this answer




























                            up vote
                            1
                            down vote













                            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)





                            share|improve this answer






























                              up vote
                              1
                              down vote













                              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






                              share|improve this answer




























                                up vote
                                1
                                down vote













                                Here Is The Code To ScrollTableView To Top Programatically



                                Swift:



                                self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)





                                share|improve this answer




























                                  up vote
                                  1
                                  down vote













                                  In Swift-3 :



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





                                  share|improve this answer




























                                    up vote
                                    1
                                    down vote













                                    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)





                                    share|improve this answer




























                                      up vote
                                      1
                                      down vote













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

                                      }

                                      }





                                      share|improve this answer




























                                        up vote
                                        1
                                        down vote













                                        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






                                        share|improve this answer




























                                          up vote
                                          0
                                          down vote













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





                                          share|improve this answer




























                                            up vote
                                            0
                                            down vote













                                            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)





                                            share|improve this answer






























                                              up vote
                                              0
                                              down vote













                                              With Swift:



                                              self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)





                                              share|improve this answer






























                                                up vote
                                                0
                                                down vote













                                                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
                                                }





                                                share|improve this answer




























                                                  up vote
                                                  0
                                                  down vote













                                                  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)





                                                  share|improve this answer




























                                                    up vote
                                                    -3
                                                    down vote













                                                    Use this code for UITableview implementation in swift:



                                                    var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
                                                    if cell == nil {
                                                    cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
                                                    }





                                                    share|improve this answer



















                                                    • 4




                                                      While this code snippet may solve the question, including an explanation 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 7:05










                                                    • The question was how to scroll UITableView to top. This post does not answer the question.
                                                      – Pang
                                                      Feb 24 '17 at 6:52















                                                    1 2
                                                    next


                                                    protected by jww Feb 21 '17 at 7:50



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?














                                                    31 Answers
                                                    31






                                                    active

                                                    oldest

                                                    votes








                                                    31 Answers
                                                    31






                                                    active

                                                    oldest

                                                    votes









                                                    active

                                                    oldest

                                                    votes






                                                    active

                                                    oldest

                                                    votes








                                                    1 2
                                                    next









                                                    up vote
                                                    772
                                                    down vote



                                                    accepted










                                                    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)





                                                    share|improve this answer



















                                                    • 9




                                                      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






                                                    • 4




                                                      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






                                                    • 3




                                                      @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 5:01






                                                    • 4




                                                      anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                      – Peter Lapisu
                                                      May 10 at 10:41






                                                    • 2




                                                      @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                      – Jordan S
                                                      Aug 6 at 7:18















                                                    up vote
                                                    772
                                                    down vote



                                                    accepted










                                                    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)





                                                    share|improve this answer



















                                                    • 9




                                                      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






                                                    • 4




                                                      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






                                                    • 3




                                                      @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 5:01






                                                    • 4




                                                      anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                      – Peter Lapisu
                                                      May 10 at 10:41






                                                    • 2




                                                      @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                      – Jordan S
                                                      Aug 6 at 7:18













                                                    up vote
                                                    772
                                                    down vote



                                                    accepted







                                                    up vote
                                                    772
                                                    down vote



                                                    accepted






                                                    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)





                                                    share|improve this answer














                                                    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)






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited May 2 at 10:27









                                                    aashish tamsya

                                                    2,60021630




                                                    2,60021630










                                                    answered Apr 7 '09 at 10:23









                                                    catlan

                                                    18.9k85464




                                                    18.9k85464








                                                    • 9




                                                      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






                                                    • 4




                                                      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






                                                    • 3




                                                      @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 5:01






                                                    • 4




                                                      anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                      – Peter Lapisu
                                                      May 10 at 10:41






                                                    • 2




                                                      @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                      – Jordan S
                                                      Aug 6 at 7:18














                                                    • 9




                                                      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






                                                    • 4




                                                      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






                                                    • 3




                                                      @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 5:01






                                                    • 4




                                                      anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                      – Peter Lapisu
                                                      May 10 at 10:41






                                                    • 2




                                                      @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                      – Jordan S
                                                      Aug 6 at 7:18








                                                    9




                                                    9




                                                    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




                                                    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




                                                    4




                                                    4




                                                    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




                                                    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




                                                    3




                                                    3




                                                    @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 5:01




                                                    @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 5:01




                                                    4




                                                    4




                                                    anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                    – Peter Lapisu
                                                    May 10 at 10:41




                                                    anyone noticed that in iOS11 this all is broken and not scrolling correctly in some cases?
                                                    – Peter Lapisu
                                                    May 10 at 10:41




                                                    2




                                                    2




                                                    @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                    – Jordan S
                                                    Aug 6 at 7:18




                                                    @PeterLapisu self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true) seems to work in iOS 11
                                                    – Jordan S
                                                    Aug 6 at 7:18












                                                    up vote
                                                    236
                                                    down vote













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





                                                    share|improve this answer



















                                                    • 12




                                                      Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                      – FreeAsInBeer
                                                      Apr 1 '12 at 2: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 2 '12 at 20:52






                                                    • 4




                                                      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 8 '13 at 20:36






                                                    • 24




                                                      The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                      – tolgamorf
                                                      May 8 '13 at 20:40






                                                    • 1




                                                      It works but doesn't take the 64px offset in regard
                                                      – Departamento B
                                                      May 20 '15 at 16:11















                                                    up vote
                                                    236
                                                    down vote













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





                                                    share|improve this answer



















                                                    • 12




                                                      Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                      – FreeAsInBeer
                                                      Apr 1 '12 at 2: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 2 '12 at 20:52






                                                    • 4




                                                      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 8 '13 at 20:36






                                                    • 24




                                                      The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                      – tolgamorf
                                                      May 8 '13 at 20:40






                                                    • 1




                                                      It works but doesn't take the 64px offset in regard
                                                      – Departamento B
                                                      May 20 '15 at 16:11













                                                    up vote
                                                    236
                                                    down vote










                                                    up vote
                                                    236
                                                    down vote









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





                                                    share|improve this answer














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






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jul 8 '16 at 6:24

























                                                    answered Jan 17 '12 at 15:24









                                                    fabb

                                                    7,711105196




                                                    7,711105196








                                                    • 12




                                                      Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                      – FreeAsInBeer
                                                      Apr 1 '12 at 2: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 2 '12 at 20:52






                                                    • 4




                                                      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 8 '13 at 20:36






                                                    • 24




                                                      The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                      – tolgamorf
                                                      May 8 '13 at 20:40






                                                    • 1




                                                      It works but doesn't take the 64px offset in regard
                                                      – Departamento B
                                                      May 20 '15 at 16:11














                                                    • 12




                                                      Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                      – FreeAsInBeer
                                                      Apr 1 '12 at 2: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 2 '12 at 20:52






                                                    • 4




                                                      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 8 '13 at 20:36






                                                    • 24




                                                      The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                      – tolgamorf
                                                      May 8 '13 at 20:40






                                                    • 1




                                                      It works but doesn't take the 64px offset in regard
                                                      – Departamento B
                                                      May 20 '15 at 16:11








                                                    12




                                                    12




                                                    Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                    – FreeAsInBeer
                                                    Apr 1 '12 at 2:29




                                                    Tomato, tomato. Hmm... That doesn't come across very clearly in writing.
                                                    – FreeAsInBeer
                                                    Apr 1 '12 at 2:29




                                                    14




                                                    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 2 '12 at 20:52




                                                    This is the best way to use when you have table header or footer views and want them to be included too.
                                                    – mafonya
                                                    Aug 2 '12 at 20:52




                                                    4




                                                    4




                                                    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 8 '13 at 20:36




                                                    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 8 '13 at 20:36




                                                    24




                                                    24




                                                    The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                    – tolgamorf
                                                    May 8 '13 at 20:40




                                                    The solution to my previous comment: [tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
                                                    – tolgamorf
                                                    May 8 '13 at 20:40




                                                    1




                                                    1




                                                    It works but doesn't take the 64px offset in regard
                                                    – Departamento B
                                                    May 20 '15 at 16:11




                                                    It works but doesn't take the 64px offset in regard
                                                    – Departamento B
                                                    May 20 '15 at 16:11










                                                    up vote
                                                    54
                                                    down vote













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





                                                    share|improve this answer



















                                                    • 5




                                                      Good solutions, scrollToFirstRow() works for me
                                                      – Kyle KIM
                                                      Mar 30 '16 at 12:46










                                                    • Thats the solution, perfect - scrollToFirstRow
                                                      – Mehul
                                                      Nov 29 '16 at 6:28












                                                    • Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                      – Vincent
                                                      Jan 30 '17 at 8:40















                                                    up vote
                                                    54
                                                    down vote













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





                                                    share|improve this answer



















                                                    • 5




                                                      Good solutions, scrollToFirstRow() works for me
                                                      – Kyle KIM
                                                      Mar 30 '16 at 12:46










                                                    • Thats the solution, perfect - scrollToFirstRow
                                                      – Mehul
                                                      Nov 29 '16 at 6:28












                                                    • Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                      – Vincent
                                                      Jan 30 '17 at 8:40













                                                    up vote
                                                    54
                                                    down vote










                                                    up vote
                                                    54
                                                    down vote









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





                                                    share|improve this answer














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






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 25 '17 at 2:08









                                                    Adrian

                                                    7,874854125




                                                    7,874854125










                                                    answered Feb 16 '16 at 9:37









                                                    A.G

                                                    10.1k7048




                                                    10.1k7048








                                                    • 5




                                                      Good solutions, scrollToFirstRow() works for me
                                                      – Kyle KIM
                                                      Mar 30 '16 at 12:46










                                                    • Thats the solution, perfect - scrollToFirstRow
                                                      – Mehul
                                                      Nov 29 '16 at 6:28












                                                    • Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                      – Vincent
                                                      Jan 30 '17 at 8:40














                                                    • 5




                                                      Good solutions, scrollToFirstRow() works for me
                                                      – Kyle KIM
                                                      Mar 30 '16 at 12:46










                                                    • Thats the solution, perfect - scrollToFirstRow
                                                      – Mehul
                                                      Nov 29 '16 at 6:28












                                                    • Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                      – Vincent
                                                      Jan 30 '17 at 8:40








                                                    5




                                                    5




                                                    Good solutions, scrollToFirstRow() works for me
                                                    – Kyle KIM
                                                    Mar 30 '16 at 12:46




                                                    Good solutions, scrollToFirstRow() works for me
                                                    – Kyle KIM
                                                    Mar 30 '16 at 12:46












                                                    Thats the solution, perfect - scrollToFirstRow
                                                    – Mehul
                                                    Nov 29 '16 at 6:28






                                                    Thats the solution, perfect - scrollToFirstRow
                                                    – Mehul
                                                    Nov 29 '16 at 6:28














                                                    Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                    – Vincent
                                                    Jan 30 '17 at 8:40




                                                    Excellent summary. When you have section headers you'll have to use option4 scrollToHeader.
                                                    – Vincent
                                                    Jan 30 '17 at 8:40










                                                    up vote
                                                    26
                                                    down vote













                                                    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.






                                                    share|improve this answer

























                                                      up vote
                                                      26
                                                      down vote













                                                      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.






                                                      share|improve this answer























                                                        up vote
                                                        26
                                                        down vote










                                                        up vote
                                                        26
                                                        down vote









                                                        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.






                                                        share|improve this answer












                                                        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.







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Aug 13 '14 at 19:53









                                                        Kof

                                                        14.4k64471




                                                        14.4k64471






















                                                            up vote
                                                            15
                                                            down vote













                                                            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)





                                                            share|improve this answer

















                                                            • 1




                                                              Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                              – finneycanhelp
                                                              Mar 12 at 19:48












                                                            • 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 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 at 1:13















                                                            up vote
                                                            15
                                                            down vote













                                                            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)





                                                            share|improve this answer

















                                                            • 1




                                                              Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                              – finneycanhelp
                                                              Mar 12 at 19:48












                                                            • 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 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 at 1:13













                                                            up vote
                                                            15
                                                            down vote










                                                            up vote
                                                            15
                                                            down vote









                                                            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)





                                                            share|improve this answer












                                                            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)






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Oct 8 '17 at 12:54









                                                            Alessandro Ornano

                                                            22.1k106079




                                                            22.1k106079








                                                            • 1




                                                              Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                              – finneycanhelp
                                                              Mar 12 at 19:48












                                                            • 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 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 at 1:13














                                                            • 1




                                                              Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                              – finneycanhelp
                                                              Mar 12 at 19:48












                                                            • 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 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 at 1:13








                                                            1




                                                            1




                                                            Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                            – finneycanhelp
                                                            Mar 12 at 19:48






                                                            Not if you have a tableHeaderView in your tableView and it's inline (scrolls with the content)
                                                            – finneycanhelp
                                                            Mar 12 at 19:48














                                                            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 at 20:55






                                                            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 at 20:55






                                                            1




                                                            1




                                                            Thank you. It will get you to the first cell. However, it doesn't show the table view header.
                                                            – finneycanhelp
                                                            Mar 14 at 1:13




                                                            Thank you. It will get you to the first cell. However, it doesn't show the table view header.
                                                            – finneycanhelp
                                                            Mar 14 at 1:13










                                                            up vote
                                                            12
                                                            down vote













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





                                                            share|improve this answer



















                                                            • 1




                                                              Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                              – johnrechd
                                                              Feb 6 at 17:00















                                                            up vote
                                                            12
                                                            down vote













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





                                                            share|improve this answer



















                                                            • 1




                                                              Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                              – johnrechd
                                                              Feb 6 at 17:00













                                                            up vote
                                                            12
                                                            down vote










                                                            up vote
                                                            12
                                                            down vote









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





                                                            share|improve this answer














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






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Apr 12 '16 at 7:36









                                                            EI Captain v2.0

                                                            17.9k96081




                                                            17.9k96081










                                                            answered Jun 17 '15 at 10:33









                                                            Eran Goldin

                                                            745916




                                                            745916








                                                            • 1




                                                              Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                              – johnrechd
                                                              Feb 6 at 17:00














                                                            • 1




                                                              Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                              – johnrechd
                                                              Feb 6 at 17:00








                                                            1




                                                            1




                                                            Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                            – johnrechd
                                                            Feb 6 at 17:00




                                                            Thank you, I kept setting it to CGPointZero and could not understand why this was happening.
                                                            – johnrechd
                                                            Feb 6 at 17:00










                                                            up vote
                                                            10
                                                            down vote













                                                            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)];





                                                            share|improve this answer























                                                            • This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                              – Felipe
                                                              Aug 31 '14 at 1:56










                                                            • This code is very useful to make the section scroll top
                                                              – srinivas n
                                                              Sep 18 '14 at 12:01















                                                            up vote
                                                            10
                                                            down vote













                                                            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)];





                                                            share|improve this answer























                                                            • This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                              – Felipe
                                                              Aug 31 '14 at 1:56










                                                            • This code is very useful to make the section scroll top
                                                              – srinivas n
                                                              Sep 18 '14 at 12:01













                                                            up vote
                                                            10
                                                            down vote










                                                            up vote
                                                            10
                                                            down vote









                                                            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)];





                                                            share|improve this answer














                                                            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)];






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Dec 22 '16 at 20:33









                                                            dreamzor

                                                            4,98733054




                                                            4,98733054










                                                            answered Apr 26 '13 at 10:17









                                                            Ramesh Muthe

                                                            798615




                                                            798615












                                                            • This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                              – Felipe
                                                              Aug 31 '14 at 1:56










                                                            • This code is very useful to make the section scroll top
                                                              – srinivas n
                                                              Sep 18 '14 at 12:01


















                                                            • This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                              – Felipe
                                                              Aug 31 '14 at 1:56










                                                            • This code is very useful to make the section scroll top
                                                              – srinivas n
                                                              Sep 18 '14 at 12:01
















                                                            This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                            – Felipe
                                                            Aug 31 '14 at 1:56




                                                            This was the simplest code and it worked OK. I actually putted CGPointMake(0.0f, 0.0f). Cheers!
                                                            – Felipe
                                                            Aug 31 '14 at 1:56












                                                            This code is very useful to make the section scroll top
                                                            – srinivas n
                                                            Sep 18 '14 at 12:01




                                                            This code is very useful to make the section scroll top
                                                            – srinivas n
                                                            Sep 18 '14 at 12:01










                                                            up vote
                                                            9
                                                            down vote













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





                                                            share|improve this answer

























                                                              up vote
                                                              9
                                                              down vote













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





                                                              share|improve this answer























                                                                up vote
                                                                9
                                                                down vote










                                                                up vote
                                                                9
                                                                down vote









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





                                                                share|improve this answer












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






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Jan 18 at 7:14









                                                                Thanh Pham

                                                                1,1491121




                                                                1,1491121






















                                                                    up vote
                                                                    8
                                                                    down vote













                                                                    Swift:



                                                                    tableView.setContentOffset(CGPointZero, animated: true)





                                                                    share|improve this answer

























                                                                      up vote
                                                                      8
                                                                      down vote













                                                                      Swift:



                                                                      tableView.setContentOffset(CGPointZero, animated: true)





                                                                      share|improve this answer























                                                                        up vote
                                                                        8
                                                                        down vote










                                                                        up vote
                                                                        8
                                                                        down vote









                                                                        Swift:



                                                                        tableView.setContentOffset(CGPointZero, animated: true)





                                                                        share|improve this answer












                                                                        Swift:



                                                                        tableView.setContentOffset(CGPointZero, animated: true)






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Dec 7 '14 at 15:04









                                                                        Esqarrouth

                                                                        23.9k11115129




                                                                        23.9k11115129






















                                                                            up vote
                                                                            8
                                                                            down vote













                                                                            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)





                                                                            share|improve this answer



























                                                                              up vote
                                                                              8
                                                                              down vote













                                                                              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)





                                                                              share|improve this answer

























                                                                                up vote
                                                                                8
                                                                                down vote










                                                                                up vote
                                                                                8
                                                                                down vote









                                                                                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)





                                                                                share|improve this answer














                                                                                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)






                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Apr 11 '17 at 4:55

























                                                                                answered Sep 7 '15 at 17:21









                                                                                ocwang

                                                                                60257




                                                                                60257






















                                                                                    up vote
                                                                                    7
                                                                                    down vote













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





                                                                                    share|improve this answer

























                                                                                      up vote
                                                                                      7
                                                                                      down vote













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





                                                                                      share|improve this answer























                                                                                        up vote
                                                                                        7
                                                                                        down vote










                                                                                        up vote
                                                                                        7
                                                                                        down vote









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





                                                                                        share|improve this answer












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






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Jan 11 at 8:23









                                                                                        Исмаил Хасбулатов

                                                                                        303212




                                                                                        303212






















                                                                                            up vote
                                                                                            6
                                                                                            down vote













                                                                                            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)





                                                                                            share|improve this answer

























                                                                                              up vote
                                                                                              6
                                                                                              down vote













                                                                                              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)





                                                                                              share|improve this answer























                                                                                                up vote
                                                                                                6
                                                                                                down vote










                                                                                                up vote
                                                                                                6
                                                                                                down vote









                                                                                                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)





                                                                                                share|improve this answer












                                                                                                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)






                                                                                                share|improve this answer












                                                                                                share|improve this answer



                                                                                                share|improve this answer










                                                                                                answered Feb 4 '15 at 8:23









                                                                                                jmcastel

                                                                                                77461433




                                                                                                77461433






















                                                                                                    up vote
                                                                                                    5
                                                                                                    down vote













                                                                                                    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() {
                                                                                                    let indexPath = IndexPath(row: 0, section: 0)
                                                                                                    if self.hasRowAtIndexPath(indexPath: indexPath) {
                                                                                                    self.scrollToRow(at: indexPath, at: .top, animated: true)
                                                                                                    }
                                                                                                    }
                                                                                                    }


                                                                                                    Usage:



                                                                                                    // from yourViewController or yourTableViewController
                                                                                                    tableView.scrollToTop()





                                                                                                    share|improve this answer



























                                                                                                      up vote
                                                                                                      5
                                                                                                      down vote













                                                                                                      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() {
                                                                                                      let indexPath = IndexPath(row: 0, section: 0)
                                                                                                      if self.hasRowAtIndexPath(indexPath: indexPath) {
                                                                                                      self.scrollToRow(at: indexPath, at: .top, animated: true)
                                                                                                      }
                                                                                                      }
                                                                                                      }


                                                                                                      Usage:



                                                                                                      // from yourViewController or yourTableViewController
                                                                                                      tableView.scrollToTop()





                                                                                                      share|improve this answer

























                                                                                                        up vote
                                                                                                        5
                                                                                                        down vote










                                                                                                        up vote
                                                                                                        5
                                                                                                        down vote









                                                                                                        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() {
                                                                                                        let indexPath = IndexPath(row: 0, section: 0)
                                                                                                        if self.hasRowAtIndexPath(indexPath: indexPath) {
                                                                                                        self.scrollToRow(at: indexPath, at: .top, animated: true)
                                                                                                        }
                                                                                                        }
                                                                                                        }


                                                                                                        Usage:



                                                                                                        // from yourViewController or yourTableViewController
                                                                                                        tableView.scrollToTop()





                                                                                                        share|improve this answer














                                                                                                        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() {
                                                                                                        let indexPath = IndexPath(row: 0, section: 0)
                                                                                                        if self.hasRowAtIndexPath(indexPath: indexPath) {
                                                                                                        self.scrollToRow(at: indexPath, at: .top, animated: true)
                                                                                                        }
                                                                                                        }
                                                                                                        }


                                                                                                        Usage:



                                                                                                        // from yourViewController or yourTableViewController
                                                                                                        tableView.scrollToTop()






                                                                                                        share|improve this answer














                                                                                                        share|improve this answer



                                                                                                        share|improve this answer








                                                                                                        edited Aug 16 at 19:57









                                                                                                        MobileMon

                                                                                                        4,93243558




                                                                                                        4,93243558










                                                                                                        answered Sep 26 '17 at 23:50









                                                                                                        Adrian

                                                                                                        7,874854125




                                                                                                        7,874854125






















                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote













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





                                                                                                            share|improve this answer





















                                                                                                            • this was more useful to me
                                                                                                              – Tejzeratul
                                                                                                              Aug 10 '17 at 22:06










                                                                                                            • great answer, appreciate it
                                                                                                              – Jeremy Bader
                                                                                                              Aug 14 '17 at 1:45















                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote













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





                                                                                                            share|improve this answer





















                                                                                                            • this was more useful to me
                                                                                                              – Tejzeratul
                                                                                                              Aug 10 '17 at 22:06










                                                                                                            • great answer, appreciate it
                                                                                                              – Jeremy Bader
                                                                                                              Aug 14 '17 at 1:45













                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote










                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote









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





                                                                                                            share|improve this answer












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






                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered Feb 1 '17 at 8:37









                                                                                                            budidino

                                                                                                            5,47055264




                                                                                                            5,47055264












                                                                                                            • this was more useful to me
                                                                                                              – Tejzeratul
                                                                                                              Aug 10 '17 at 22:06










                                                                                                            • great answer, appreciate it
                                                                                                              – Jeremy Bader
                                                                                                              Aug 14 '17 at 1:45


















                                                                                                            • this was more useful to me
                                                                                                              – Tejzeratul
                                                                                                              Aug 10 '17 at 22:06










                                                                                                            • great answer, appreciate it
                                                                                                              – Jeremy Bader
                                                                                                              Aug 14 '17 at 1:45
















                                                                                                            this was more useful to me
                                                                                                            – Tejzeratul
                                                                                                            Aug 10 '17 at 22:06




                                                                                                            this was more useful to me
                                                                                                            – Tejzeratul
                                                                                                            Aug 10 '17 at 22:06












                                                                                                            great answer, appreciate it
                                                                                                            – Jeremy Bader
                                                                                                            Aug 14 '17 at 1:45




                                                                                                            great answer, appreciate it
                                                                                                            – Jeremy Bader
                                                                                                            Aug 14 '17 at 1:45










                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote













                                                                                                            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)





                                                                                                            share|improve this answer





















                                                                                                            • This was the part I was missing.
                                                                                                              – That Guy
                                                                                                              Jul 20 '17 at 12:50















                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote













                                                                                                            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)





                                                                                                            share|improve this answer





















                                                                                                            • This was the part I was missing.
                                                                                                              – That Guy
                                                                                                              Jul 20 '17 at 12:50













                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote










                                                                                                            up vote
                                                                                                            4
                                                                                                            down vote









                                                                                                            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)





                                                                                                            share|improve this answer












                                                                                                            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)






                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered May 4 '17 at 19:01









                                                                                                            Jacob Caraballo

                                                                                                            2,38221524




                                                                                                            2,38221524












                                                                                                            • This was the part I was missing.
                                                                                                              – That Guy
                                                                                                              Jul 20 '17 at 12:50


















                                                                                                            • This was the part I was missing.
                                                                                                              – That Guy
                                                                                                              Jul 20 '17 at 12:50
















                                                                                                            This was the part I was missing.
                                                                                                            – That Guy
                                                                                                            Jul 20 '17 at 12:50




                                                                                                            This was the part I was missing.
                                                                                                            – That Guy
                                                                                                            Jul 20 '17 at 12:50










                                                                                                            up vote
                                                                                                            2
                                                                                                            down vote













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





                                                                                                            share|improve this answer

























                                                                                                              up vote
                                                                                                              2
                                                                                                              down vote













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





                                                                                                              share|improve this answer























                                                                                                                up vote
                                                                                                                2
                                                                                                                down vote










                                                                                                                up vote
                                                                                                                2
                                                                                                                down vote









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





                                                                                                                share|improve this answer












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






                                                                                                                share|improve this answer












                                                                                                                share|improve this answer



                                                                                                                share|improve this answer










                                                                                                                answered Sep 22 '17 at 20:32









                                                                                                                Hans Brende

                                                                                                                3,31531932




                                                                                                                3,31531932






















                                                                                                                    up vote
                                                                                                                    2
                                                                                                                    down vote













                                                                                                                    Swift 4 via extension, handles empty table view:



                                                                                                                    extension UITableView {
                                                                                                                    func scrollToTop(animated: Bool) {
                                                                                                                    self.setContentOffset(CGPoint.zero, animated: animated);
                                                                                                                    }
                                                                                                                    }





                                                                                                                    share|improve this answer

























                                                                                                                      up vote
                                                                                                                      2
                                                                                                                      down vote













                                                                                                                      Swift 4 via extension, handles empty table view:



                                                                                                                      extension UITableView {
                                                                                                                      func scrollToTop(animated: Bool) {
                                                                                                                      self.setContentOffset(CGPoint.zero, animated: animated);
                                                                                                                      }
                                                                                                                      }





                                                                                                                      share|improve this answer























                                                                                                                        up vote
                                                                                                                        2
                                                                                                                        down vote










                                                                                                                        up vote
                                                                                                                        2
                                                                                                                        down vote









                                                                                                                        Swift 4 via extension, handles empty table view:



                                                                                                                        extension UITableView {
                                                                                                                        func scrollToTop(animated: Bool) {
                                                                                                                        self.setContentOffset(CGPoint.zero, animated: animated);
                                                                                                                        }
                                                                                                                        }





                                                                                                                        share|improve this answer












                                                                                                                        Swift 4 via extension, handles empty table view:



                                                                                                                        extension UITableView {
                                                                                                                        func scrollToTop(animated: Bool) {
                                                                                                                        self.setContentOffset(CGPoint.zero, animated: animated);
                                                                                                                        }
                                                                                                                        }






                                                                                                                        share|improve this answer












                                                                                                                        share|improve this answer



                                                                                                                        share|improve this answer










                                                                                                                        answered Feb 20 at 6:23









                                                                                                                        Shawn

                                                                                                                        818




                                                                                                                        818






















                                                                                                                            up vote
                                                                                                                            1
                                                                                                                            down vote













                                                                                                                            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)





                                                                                                                            share|improve this answer



























                                                                                                                              up vote
                                                                                                                              1
                                                                                                                              down vote













                                                                                                                              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)





                                                                                                                              share|improve this answer

























                                                                                                                                up vote
                                                                                                                                1
                                                                                                                                down vote










                                                                                                                                up vote
                                                                                                                                1
                                                                                                                                down vote









                                                                                                                                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)





                                                                                                                                share|improve this answer














                                                                                                                                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)






                                                                                                                                share|improve this answer














                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer








                                                                                                                                edited Nov 12 '15 at 23:58









                                                                                                                                Rohit Gupta

                                                                                                                                2,20891835




                                                                                                                                2,20891835










                                                                                                                                answered Nov 12 '15 at 23:37









                                                                                                                                Carlos.V

                                                                                                                                1309




                                                                                                                                1309






















                                                                                                                                    up vote
                                                                                                                                    1
                                                                                                                                    down vote













                                                                                                                                    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






                                                                                                                                    share|improve this answer

























                                                                                                                                      up vote
                                                                                                                                      1
                                                                                                                                      down vote













                                                                                                                                      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






                                                                                                                                      share|improve this answer























                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote










                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote









                                                                                                                                        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






                                                                                                                                        share|improve this answer












                                                                                                                                        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







                                                                                                                                        share|improve this answer












                                                                                                                                        share|improve this answer



                                                                                                                                        share|improve this answer










                                                                                                                                        answered Jan 4 '17 at 6:24









                                                                                                                                        Sayali Shinde

                                                                                                                                        563




                                                                                                                                        563






















                                                                                                                                            up vote
                                                                                                                                            1
                                                                                                                                            down vote













                                                                                                                                            Here Is The Code To ScrollTableView To Top Programatically



                                                                                                                                            Swift:



                                                                                                                                            self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)





                                                                                                                                            share|improve this answer

























                                                                                                                                              up vote
                                                                                                                                              1
                                                                                                                                              down vote













                                                                                                                                              Here Is The Code To ScrollTableView To Top Programatically



                                                                                                                                              Swift:



                                                                                                                                              self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)





                                                                                                                                              share|improve this answer























                                                                                                                                                up vote
                                                                                                                                                1
                                                                                                                                                down vote










                                                                                                                                                up vote
                                                                                                                                                1
                                                                                                                                                down vote









                                                                                                                                                Here Is The Code To ScrollTableView To Top Programatically



                                                                                                                                                Swift:



                                                                                                                                                self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)





                                                                                                                                                share|improve this answer












                                                                                                                                                Here Is The Code To ScrollTableView To Top Programatically



                                                                                                                                                Swift:



                                                                                                                                                self.TableView.setContentOffset(CGPointMake(0, 1), animated:true)






                                                                                                                                                share|improve this answer












                                                                                                                                                share|improve this answer



                                                                                                                                                share|improve this answer










                                                                                                                                                answered Jan 4 '17 at 6:29









                                                                                                                                                Kavin Kumar Arumugam

                                                                                                                                                8551634




                                                                                                                                                8551634






















                                                                                                                                                    up vote
                                                                                                                                                    1
                                                                                                                                                    down vote













                                                                                                                                                    In Swift-3 :



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





                                                                                                                                                    share|improve this answer

























                                                                                                                                                      up vote
                                                                                                                                                      1
                                                                                                                                                      down vote













                                                                                                                                                      In Swift-3 :



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





                                                                                                                                                      share|improve this answer























                                                                                                                                                        up vote
                                                                                                                                                        1
                                                                                                                                                        down vote










                                                                                                                                                        up vote
                                                                                                                                                        1
                                                                                                                                                        down vote









                                                                                                                                                        In Swift-3 :



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





                                                                                                                                                        share|improve this answer












                                                                                                                                                        In Swift-3 :



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






                                                                                                                                                        share|improve this answer












                                                                                                                                                        share|improve this answer



                                                                                                                                                        share|improve this answer










                                                                                                                                                        answered Jan 24 '17 at 7:41









                                                                                                                                                        Jeni Khant

                                                                                                                                                        10310




                                                                                                                                                        10310






















                                                                                                                                                            up vote
                                                                                                                                                            1
                                                                                                                                                            down vote













                                                                                                                                                            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)





                                                                                                                                                            share|improve this answer

























                                                                                                                                                              up vote
                                                                                                                                                              1
                                                                                                                                                              down vote













                                                                                                                                                              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)





                                                                                                                                                              share|improve this answer























                                                                                                                                                                up vote
                                                                                                                                                                1
                                                                                                                                                                down vote










                                                                                                                                                                up vote
                                                                                                                                                                1
                                                                                                                                                                down vote









                                                                                                                                                                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)





                                                                                                                                                                share|improve this answer












                                                                                                                                                                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)






                                                                                                                                                                share|improve this answer












                                                                                                                                                                share|improve this answer



                                                                                                                                                                share|improve this answer










                                                                                                                                                                answered Jun 12 '17 at 4:41









                                                                                                                                                                Gulz

                                                                                                                                                                888713




                                                                                                                                                                888713






















                                                                                                                                                                    up vote
                                                                                                                                                                    1
                                                                                                                                                                    down vote













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

                                                                                                                                                                    }

                                                                                                                                                                    }





                                                                                                                                                                    share|improve this answer

























                                                                                                                                                                      up vote
                                                                                                                                                                      1
                                                                                                                                                                      down vote













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

                                                                                                                                                                      }

                                                                                                                                                                      }





                                                                                                                                                                      share|improve this answer























                                                                                                                                                                        up vote
                                                                                                                                                                        1
                                                                                                                                                                        down vote










                                                                                                                                                                        up vote
                                                                                                                                                                        1
                                                                                                                                                                        down vote









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

                                                                                                                                                                        }

                                                                                                                                                                        }





                                                                                                                                                                        share|improve this answer












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

                                                                                                                                                                        }

                                                                                                                                                                        }






                                                                                                                                                                        share|improve this answer












                                                                                                                                                                        share|improve this answer



                                                                                                                                                                        share|improve this answer










                                                                                                                                                                        answered Jun 7 at 10:22









                                                                                                                                                                        Okan Yücel

                                                                                                                                                                        1936




                                                                                                                                                                        1936






















                                                                                                                                                                            up vote
                                                                                                                                                                            1
                                                                                                                                                                            down vote













                                                                                                                                                                            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






                                                                                                                                                                            share|improve this answer

























                                                                                                                                                                              up vote
                                                                                                                                                                              1
                                                                                                                                                                              down vote













                                                                                                                                                                              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






                                                                                                                                                                              share|improve this answer























                                                                                                                                                                                up vote
                                                                                                                                                                                1
                                                                                                                                                                                down vote










                                                                                                                                                                                up vote
                                                                                                                                                                                1
                                                                                                                                                                                down vote









                                                                                                                                                                                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






                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                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







                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                answered Jul 10 at 5:14









                                                                                                                                                                                jimmyruby

                                                                                                                                                                                277




                                                                                                                                                                                277






















                                                                                                                                                                                    up vote
                                                                                                                                                                                    0
                                                                                                                                                                                    down vote













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





                                                                                                                                                                                    share|improve this answer

























                                                                                                                                                                                      up vote
                                                                                                                                                                                      0
                                                                                                                                                                                      down vote













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





                                                                                                                                                                                      share|improve this answer























                                                                                                                                                                                        up vote
                                                                                                                                                                                        0
                                                                                                                                                                                        down vote










                                                                                                                                                                                        up vote
                                                                                                                                                                                        0
                                                                                                                                                                                        down vote









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





                                                                                                                                                                                        share|improve this answer












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






                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                        share|improve this answer










                                                                                                                                                                                        answered Nov 12 '14 at 3:58









                                                                                                                                                                                        Matheus Veloza

                                                                                                                                                                                        39234




                                                                                                                                                                                        39234






















                                                                                                                                                                                            up vote
                                                                                                                                                                                            0
                                                                                                                                                                                            down vote













                                                                                                                                                                                            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)





                                                                                                                                                                                            share|improve this answer



























                                                                                                                                                                                              up vote
                                                                                                                                                                                              0
                                                                                                                                                                                              down vote













                                                                                                                                                                                              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)





                                                                                                                                                                                              share|improve this answer

























                                                                                                                                                                                                up vote
                                                                                                                                                                                                0
                                                                                                                                                                                                down vote










                                                                                                                                                                                                up vote
                                                                                                                                                                                                0
                                                                                                                                                                                                down vote









                                                                                                                                                                                                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)





                                                                                                                                                                                                share|improve this answer














                                                                                                                                                                                                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)






                                                                                                                                                                                                share|improve this answer














                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                share|improve this answer








                                                                                                                                                                                                edited Feb 29 '16 at 9:07









                                                                                                                                                                                                Daniel Galasko

                                                                                                                                                                                                16.9k65886




                                                                                                                                                                                                16.9k65886










                                                                                                                                                                                                answered Aug 31 '15 at 10:37









                                                                                                                                                                                                idris yıldız

                                                                                                                                                                                                1,7531415




                                                                                                                                                                                                1,7531415






















                                                                                                                                                                                                    up vote
                                                                                                                                                                                                    0
                                                                                                                                                                                                    down vote













                                                                                                                                                                                                    With Swift:



                                                                                                                                                                                                    self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)





                                                                                                                                                                                                    share|improve this answer



























                                                                                                                                                                                                      up vote
                                                                                                                                                                                                      0
                                                                                                                                                                                                      down vote













                                                                                                                                                                                                      With Swift:



                                                                                                                                                                                                      self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)





                                                                                                                                                                                                      share|improve this answer

























                                                                                                                                                                                                        up vote
                                                                                                                                                                                                        0
                                                                                                                                                                                                        down vote










                                                                                                                                                                                                        up vote
                                                                                                                                                                                                        0
                                                                                                                                                                                                        down vote









                                                                                                                                                                                                        With Swift:



                                                                                                                                                                                                        self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)





                                                                                                                                                                                                        share|improve this answer














                                                                                                                                                                                                        With Swift:



                                                                                                                                                                                                        self.scripSearchView.quickListTbl?.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)






                                                                                                                                                                                                        share|improve this answer














                                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                                        share|improve this answer








                                                                                                                                                                                                        edited Apr 14 '17 at 8:10









                                                                                                                                                                                                        Aurasphere

                                                                                                                                                                                                        2,489102850




                                                                                                                                                                                                        2,489102850










                                                                                                                                                                                                        answered Apr 14 '17 at 6:36









                                                                                                                                                                                                        Dhananjay Patil

                                                                                                                                                                                                        1506




                                                                                                                                                                                                        1506






















                                                                                                                                                                                                            up vote
                                                                                                                                                                                                            0
                                                                                                                                                                                                            down vote













                                                                                                                                                                                                            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
                                                                                                                                                                                                            }





                                                                                                                                                                                                            share|improve this answer

























                                                                                                                                                                                                              up vote
                                                                                                                                                                                                              0
                                                                                                                                                                                                              down vote













                                                                                                                                                                                                              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
                                                                                                                                                                                                              }





                                                                                                                                                                                                              share|improve this answer























                                                                                                                                                                                                                up vote
                                                                                                                                                                                                                0
                                                                                                                                                                                                                down vote










                                                                                                                                                                                                                up vote
                                                                                                                                                                                                                0
                                                                                                                                                                                                                down vote









                                                                                                                                                                                                                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
                                                                                                                                                                                                                }





                                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                                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
                                                                                                                                                                                                                }






                                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                                                answered Jun 7 '17 at 15:45









                                                                                                                                                                                                                YannickSteph

                                                                                                                                                                                                                7,35313836




                                                                                                                                                                                                                7,35313836






















                                                                                                                                                                                                                    up vote
                                                                                                                                                                                                                    0
                                                                                                                                                                                                                    down vote













                                                                                                                                                                                                                    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)





                                                                                                                                                                                                                    share|improve this answer

























                                                                                                                                                                                                                      up vote
                                                                                                                                                                                                                      0
                                                                                                                                                                                                                      down vote













                                                                                                                                                                                                                      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)





                                                                                                                                                                                                                      share|improve this answer























                                                                                                                                                                                                                        up vote
                                                                                                                                                                                                                        0
                                                                                                                                                                                                                        down vote










                                                                                                                                                                                                                        up vote
                                                                                                                                                                                                                        0
                                                                                                                                                                                                                        down vote









                                                                                                                                                                                                                        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)





                                                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                                                        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)






                                                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                                                        share|improve this answer










                                                                                                                                                                                                                        answered Nov 11 at 3:49









                                                                                                                                                                                                                        ScottyBlades

                                                                                                                                                                                                                        1,3501421




                                                                                                                                                                                                                        1,3501421






















                                                                                                                                                                                                                            up vote
                                                                                                                                                                                                                            -3
                                                                                                                                                                                                                            down vote













                                                                                                                                                                                                                            Use this code for UITableview implementation in swift:



                                                                                                                                                                                                                            var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
                                                                                                                                                                                                                            if cell == nil {
                                                                                                                                                                                                                            cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
                                                                                                                                                                                                                            }





                                                                                                                                                                                                                            share|improve this answer



















                                                                                                                                                                                                                            • 4




                                                                                                                                                                                                                              While this code snippet may solve the question, including an explanation 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 7:05










                                                                                                                                                                                                                            • The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                              – Pang
                                                                                                                                                                                                                              Feb 24 '17 at 6:52















                                                                                                                                                                                                                            up vote
                                                                                                                                                                                                                            -3
                                                                                                                                                                                                                            down vote













                                                                                                                                                                                                                            Use this code for UITableview implementation in swift:



                                                                                                                                                                                                                            var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
                                                                                                                                                                                                                            if cell == nil {
                                                                                                                                                                                                                            cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
                                                                                                                                                                                                                            }





                                                                                                                                                                                                                            share|improve this answer



















                                                                                                                                                                                                                            • 4




                                                                                                                                                                                                                              While this code snippet may solve the question, including an explanation 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 7:05










                                                                                                                                                                                                                            • The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                              – Pang
                                                                                                                                                                                                                              Feb 24 '17 at 6:52













                                                                                                                                                                                                                            up vote
                                                                                                                                                                                                                            -3
                                                                                                                                                                                                                            down vote










                                                                                                                                                                                                                            up vote
                                                                                                                                                                                                                            -3
                                                                                                                                                                                                                            down vote









                                                                                                                                                                                                                            Use this code for UITableview implementation in swift:



                                                                                                                                                                                                                            var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
                                                                                                                                                                                                                            if cell == nil {
                                                                                                                                                                                                                            cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
                                                                                                                                                                                                                            }





                                                                                                                                                                                                                            share|improve this answer














                                                                                                                                                                                                                            Use this code for UITableview implementation in swift:



                                                                                                                                                                                                                            var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
                                                                                                                                                                                                                            if cell == nil {
                                                                                                                                                                                                                            cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
                                                                                                                                                                                                                            }






                                                                                                                                                                                                                            share|improve this answer














                                                                                                                                                                                                                            share|improve this answer



                                                                                                                                                                                                                            share|improve this answer








                                                                                                                                                                                                                            edited Feb 21 '17 at 9:27









                                                                                                                                                                                                                            anacron

                                                                                                                                                                                                                            4,13111527




                                                                                                                                                                                                                            4,13111527










                                                                                                                                                                                                                            answered Feb 21 '17 at 6:41









                                                                                                                                                                                                                            Pradnya

                                                                                                                                                                                                                            71




                                                                                                                                                                                                                            71








                                                                                                                                                                                                                            • 4




                                                                                                                                                                                                                              While this code snippet may solve the question, including an explanation 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 7:05










                                                                                                                                                                                                                            • The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                              – Pang
                                                                                                                                                                                                                              Feb 24 '17 at 6:52














                                                                                                                                                                                                                            • 4




                                                                                                                                                                                                                              While this code snippet may solve the question, including an explanation 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 7:05










                                                                                                                                                                                                                            • The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                              – Pang
                                                                                                                                                                                                                              Feb 24 '17 at 6:52








                                                                                                                                                                                                                            4




                                                                                                                                                                                                                            4




                                                                                                                                                                                                                            While this code snippet may solve the question, including an explanation 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 7:05




                                                                                                                                                                                                                            While this code snippet may solve the question, including an explanation 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 7:05












                                                                                                                                                                                                                            The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                            – Pang
                                                                                                                                                                                                                            Feb 24 '17 at 6:52




                                                                                                                                                                                                                            The question was how to scroll UITableView to top. This post does not answer the question.
                                                                                                                                                                                                                            – Pang
                                                                                                                                                                                                                            Feb 24 '17 at 6:52










                                                                                                                                                                                                                            1 2
                                                                                                                                                                                                                            next




                                                                                                                                                                                                                            protected by jww Feb 21 '17 at 7:50



                                                                                                                                                                                                                            Thank you for your interest in this question.
                                                                                                                                                                                                                            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                                                                                            Would you like to answer one of these unanswered questions instead?



                                                                                                                                                                                                                            Popular posts from this blog

                                                                                                                                                                                                                            List item for chat from Array inside array React Native

                                                                                                                                                                                                                            Thiostrepton

                                                                                                                                                                                                                            Caerphilly