PageViewController current page index in Swift

前端 未结 12 2107
你的背包
你的背包 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:24

    First, have your UIPageViewController implement the UIPageViewControllerDataSource method presentationIndex(for pageViewController: UIPageViewController) -> Int to return the index for each of the PageViewController's ViewControllers.

    Then in your delegate, access the datasource through the passed-in PageViewController:

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        guard let dataSource = pageViewController.dataSource, 
              let currentIndex = dataSource.presentationIndex?(for: pageViewController) else { preconditionFailure() }
    
    }
    

提交回复
热议问题