PageViewController current page index in Swift

前端 未结 12 2106
你的背包
你的背包 2021-02-01 15:37

I want to get current index of a pageViewController, I don\'t know how I get the visible pages index.

func pageViewController(pageViewController: UIPageViewContr         


        
12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 16:21

    In Swift 3

    Override didFinishAnimating function of UIPageViewControllerDelegate like this:

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            if let currentViewController = pageViewController.viewControllers![0] as? WalkthroughContentViewController {
                pageControl.currentPage = currentViewController.index
            }
        }
    }
    

    where WalkthroughContentViewController is the UIViewController presented by UIPageViewController

    Remember to keep an index variable inside the WalkthroughContentViewController. Also, in the viewDidLoad method set:

    delegate = self
    

提交回复
热议问题