Go to the top of the page UITableView with double click on UITabBarItem

I want to simulate the behavior of twitter or instagram in their list:

By double-clicking in UITabBarItem, the list scrolls up. Does anyone know how I can do this? I have 4 elements in my UITabBarController, all of them are a list, so I want to do this for everyone.

The problem is that when I click the bar element in the push view from the mi list, this function calls and detects a double click before getting the controller of the root navigation view.

I hope that with my problem it can be clear.

var previousController: UIViewController?

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    if previousController == viewController{
        if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {
            vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
            print("same")
        }
    }else{
        print("No same")
    }
    previousController = viewController
}

The behavior is wrong. When I exit a view using a tab bar item, the root view is on cell 0

enter image description here

+4
3

, , - , viewController , , .

:

var previousController: UIViewController?

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    if previousController == viewController {
        if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {

            if vc.isViewLoaded() && (vc.view.window != nil) {
                // viewController is visible
                vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
            }

            print("same")
        }
    }else{
        print("No same")
    }

    previousController = viewController

}
+9

Swift 3

extension UIViewController {
    func scrollToTop() {
        func scrollToTop(view: UIView?) {
            guard let view = view else { return }

            switch view {
            case let scrollView as UIScrollView:
                if scrollView.scrollsToTop == true {
                    scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
                    return
                }
            default:
                break
            }

            for subView in view.subviews {
                scrollToTop(view: subView)
            }
        }

        scrollToTop(view: self.view)
    }
}

var previousController: UIViewController?

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

        //when user is in FeedViewController and scroll at last record and want to
        if previousController == viewController {
            if let navVC = viewController as? UINavigationController, let vc = navVC.viewControllers.first as? HomeViewController {

                if vc.isViewLoaded && (vc.view.window != nil) {
                    vc.scrollToTop()
                }
                print("same")
            }
        }
        else{
            print("No same")
        }

        previousController = viewController
        return true;
    }

. @Olivier Wilkinson , , . ( )

+1

Olivier Wilkinsons , Swift 4,

vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)

vc.tableBusinessList.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)

+1

All Articles