UIPageViewController with different ViewControllers, right way?

后端 未结 4 1258
半阙折子戏
半阙折子戏 2021-01-03 02:25

Basically, I have a Table View Controller and a normal View Controller, I want to make it so that I can swipe between the view controllers like on the home screen of app (ob

4条回答
  •  攒了一身酷
    2021-01-03 03:11

    This is an old question but since I bumped into this problem I'll post my solution.

    The first time I did this I added my code

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { }
    

    Which received problems when you continuously scroll the views using two fingers and didFinishAnimating is not called unless scrolling stops.

    So I just used this delegate method instead.

    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
    
        let currentView = pendingViewControllers[0]
    
        if currentView is UINavigationController && currentView.restorationIdentifier == "Settings" {
            index = 0
        } else if currentView is UINavigationController && currentView.restorationIdentifier == "Main" {
            index  = 1
        } else if currentView is UINavigationController && currentView.restorationIdentifier == "Message" {
            index = 2
        }
    }
    

    Where currentView is the currentUIViewController (or UINavigationController like what I am using, using a restorationIdentifier to identify my navigation controller as well), and index is a global value to know which UIViewController in the array to instantiate.

提交回复
热议问题