PageViewController current page index in Swift

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

    You can use next method:

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        guard
            completed,
            let viewControllerIndex = tutorialViews.index(of: pageViewController.viewControllers!.first!) else
                return
            }
    
        self.currentIndex = viewControllerIndex
    }
    

    Where tutorialViews is an array of pages (ViewControllers).
    And initialize currentIndex like that:

    var currentIndex = 0 {
        didSet {
            self.updateContentForPage(withIndex: currentIndex)
        }
    }
    

提交回复
热议问题