iosuitableviewdatasourceuipageviewcontrolleruipagecontrol

UIPageViewController -presentationCount not firing


Problem: UIPageViewController navigation between pages works fine but:
1) Navigation dots view not showing up
2) Some dataSource methods not called (probably cause for 1.)

I have following UI setup (basically UIPageViewController in UITableViewCell)

UITableViewCell <- UIViewController <- ContainerView <- UIPageViewController

Problem:

CALLED OK:

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?

NOT CALLED:

func presentationCount(for pageViewController: UIPageViewController) -> Int
func presentationIndex(for pageViewController: UIPageViewController) -> Int 

All setup done after viewDidLoad:

dataSource = self
setViewControllers([viewController], direction: .reverse, animated: false, completion: nil)

Note: I'm using another UIPageViewController somewhere else in the app directly on the content view -> there all delegate methods are working fine. Another (probably directly related) problem is that navigation dot view is not visible. (Again works fine in case where not embedded).

Anybody experience anything like this? Why would some dataSource delegate methods be called while others would not called?

I've been looking at this code and all I could find in google for hours but can't move forward.

UIPageViewController Implementation


Solution

  • A page indicator will be visible if both methods are implemented, transition style is 'UIPageViewControllerTransitionStyleScroll', and navigation orientation is 'UIPageViewControllerNavigationOrientationHorizontal'.

    Both methods are called in response to a 'setViewControllers:...' call, but the presentation index is updated automatically in the case of gesture-driven navigation.

    According to Apple Documentation you need to implement both methods and make sure that the controller's transition style is "scroll". Then the Page Indicator will be visible and the functions are called. Please check this.

    for the visibility of the pageindicator. Add below code in viewdidload

    var pageControl = UIPageControl.appearance()
    pageControl.pageIndicatorTintColor = UIColor.lightGray
    pageControl.currentPageIndicatorTintColor = UIColor.white
    pageControl.backgroundColor = UIColor.black
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }
        
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        if let currentVC = self.viewControllers!.first {
            let currentIndex = orderedViewControllers.index(of: currentVC)
            return currentIndex!
        }else{
            return 0
        }
     }