PageViewController current page index in Swift

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

    Swift 4 version

    Extending the ViewController class with these protocols (UIPageViewControllerDelegate, UIPageViewControllerDataSource) and adding the following functions helped me to make my page control work correctly.

    class ViewController : UIPageViewControllerDelegate,UIPageViewControllerDataSource {
    
        func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
            if let viewController = pendingViewControllers[0] as? DataViewController {
                self.lastPendingViewControllerIndex = viewController.index!
            }
        }
    
        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            if completed {
                pageControl.currentPage = self.lastPendingViewControllerIndex
                if lastPendingViewControllerIndex == 4 {
                    self.getstartedButton.isHidden=false
                } else {
                    self.getstartedButton.isHidden=true
                }
            }
        }
    }
    

提交回复
热议问题