iHarshad
iHarshad

Reputation: 143

Scroll to top when tap on UITabBarController

I am having UITabBarController in which one of tab has two container view and I want to scroll to top on both controllers in it.

My Base View Controller is NotificationViewController and child view controllers are like NotificationNetworkViewController and NotificationPodcastViewController

I am Using following extension : -

extension NotificationNetworkViewController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        print("Network TAB INDEX : \(tabBarController.selectedIndex)")
        notificationTable.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }
}

Upvotes: 0

Views: 81

Answers (1)

Ayaz Rafai
Ayaz Rafai

Reputation: 281

Use the following code to achieve your needs. For animation, you just need to pass value true/false in animated of scrollToRow function. Hope this will help you!

To scroll top without animation

func scrollToBottomWithoutAnimation() {
     DispatchQueue.main.async {
         if self.dataArray.count > 0 {
             let indexPath = IndexPath(row: 0, section: 0)
             notificationTable.scrollToRow(at: indexPath, at: .top, animated: false)
         }
     }
}

To scroll top with animation

func scrollToBottomWithoutAnimation() {
     DispatchQueue.main.async {
         if self.dataArray.count > 0 {
             let indexPath = IndexPath(row: 0, section: 0)
             notificationTable.scrollToRow(at: indexPath, at: .top, animated: true)
         }
     }
}

Upvotes: 1

Related Questions