Tap tab bar to scroll to top of UITableViewController

后端 未结 12 932
面向向阳花
面向向阳花 2020-12-23 10:15

Tapping the tab bar icon for the current navigation controller already returns the user to the root view, but if they are scrolled way down, if they tap it again I want it t

12条回答
  •  醉梦人生
    2020-12-23 10:25

    I found the scrollRectToVisible method works better than the setContentOffset.

    Swift:

    After you catch the click on the tab bar from the delegate, something like below:

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
     if (viewController.isKindOfClass(SomeControllerClass) && tabBarController.selectedIndex == 0) 
          {
            viewController.scrollToTop()
          }
     }
    

    Now for the scrollToTop function inside the controller:

    func scrollToTop()
    {
        self.tableView.scrollRectToVisible(CGRectMake(0,0,CGRectGetWidth(self.tableView.frame), CGRectGetHeight(self.tableView.frame)), animated: true)
    } 
    

提交回复
热议问题