How to put the UIPageControl element on top of the sliding pages within a UIPageViewController?

后端 未结 7 1750
闹比i
闹比i 2020-12-02 10:20

Regarding to this tutorial by AppCoda about how to implement a app with UIPageViewController I\'d like to use a custom page control element on top of the pages instead of at

7条回答
  •  抹茶落季
    2020-12-02 10:46

    You have to implement a custom UIPageControl and add it to the view. As others have mentioned, view.bringSubviewToFront(pageControl) must be called.

    I have an example of a view controller with all the code on setting up a custom UIPageControl (in storyboard) with UIPageViewController

    There are 2 methods which you need to implement to set the current page indicator.

    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        pendingIndex = pages.indexOf(pendingViewControllers.first!)
    }
    
    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            currentIndex = pendingIndex
            if let index = currentIndex {
                pageControl.currentPage = index
            }
        }
    }
    

提交回复
热议问题