简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM