0

I'm using swift, I want to go to the top of a tableView upon a button press, like on twitter pressing the home button will take you to the top of your feed, so far I haven't been able to find a working solution, this is what I've tried :

tableView.setContentOffset(.zero, animated: true)

tableView.setContentOffset(CGPoint(x: 0.0, y: -tableView.contentInset.top), animated: true)

func scrollToTop(){
        let indexPath = IndexPath(row: 0, section: 0)
        tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }

All solutions will only take you to an arbitrary cell, bonus points if you know the solution to do this upon a tab bar button click too

  • In case you didn't know, tapping the status bar on iOS typically scrolls to the top of the list on screen. – fphilipe Jul 27 '19 at 06:21
  • Possible duplicate of [How to scroll to the exact end of the UITableView?](https://stackoverflow.com/questions/33705371/how-to-scroll-to-the-exact-end-of-the-uitableview) – steveSarsawa Jul 27 '19 at 06:28

3 Answers3

2

First off, I like your username.

Next, what's the problem again with the solution you posted? They're correct, and it seems there's no better way to do it with UITableView or UICollectionView other than scrollTo... built-in function. And setting offset for UIScrollView.

For the handling of tap on the tab bar, one thing you can do is to subclass the UITabBarController, and conform to its protocol, and implement UITabBarControllerDelegate desired methods, like:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {.

From there you can handle everything, and from there get the viewController object and cast it on to your home class, and scroll the view to the top.

What function I use for this? It's similar to yours, but with a little bit of a condition:

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

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

Also, try to tap the status bar of your app and see where it will take you.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • status bar works, but those solutions I tried would only take you up a few cells, not all the way to the top – Stop downvoting me please Jul 27 '19 at 18:52
  • This doesn't scroll to the top if there is a table view header and/or a section header. – rmaddy Jul 27 '19 at 21:41
  • @Stopdownvotingmeplease did you try this? : tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height), animated: true) – Celeste Jul 27 '19 at 22:29
0

I edited my answer again, no indexPath involved:

@IBOutlet weak var myButton: UIButton!

override func viewDidLoad() { 
  super.viewDidLoad
}


@IBAction func button(_ sender: UIButton) { 

  myButton.isSelected = !myButton.isSelected
  tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height), animated: true) 

}

 override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

    if myButton.isSelected {

        print("SUCCESS 1")


    }
}

override func scrollViewWillEndDragging(_ scrollView: UIScrollView,   withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if myButton.isSelected {
        myButton.isSelected = false
        print("SUCCESS 2")

    }
}
Celeste
  • 1,519
  • 6
  • 19
0

I guess the issue was some of the other logic in the button function was messing with the scrollToTop() function, this code works:

func scrollToTop(){
        tableView.setContentOffset(.zero, animated: true)
    }

    @IBAction func BlogPressed(_ sender: Any) {

        if(BlogIndicator.alpha == 1.0){
            scrollToTop()
        } else {
            articleRefresh()
            self.articleArray = self.blogArray

            BlogIndicator.alpha = 1.0
            NewsIndicator.alpha = 0.0
            RosterIndicator.alpha = 0.0
            DraftIndicator.alpha = 0.0

            tableView.reloadData()
        }
    }

But I'm still looking for a solution for going to the top of the table view on when pressing the tab bar button