1

I try the following approach found here

extension UIPageViewController {

func goToNextPage(){

    guard let currentViewController = self.viewControllers?.first else { return }

    guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }

    setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)

   }
}

It works, but there is one issue:

When the page is turned programatically, the indicator does not move. It seems that they move only when user turns. page with swipe

that's how indicators should look like after programmatic turn is performed:

enter image description here

instead they remain unchanged

enter image description here

Which leads to issue that hierarchy shown by indicators is rather [2,0,1] instead of [0,1,2]

This is how I implement indicators:

func presentationCount(for PageViewController:UIPageViewController) -> Int {


    return 3

}

func presentationIndex(for PageViewController:UIPageViewController) -> Int {

    return 0
}

How to make dots indicators move when the page is turned programatically?

ttppbb
  • 65
  • 8
theDC
  • 6,364
  • 10
  • 56
  • 98

3 Answers3

1

Unfortunately you can't update UIPageControl embedded in UIPageViewController. However, you can have your own UIPageControl in UIPageViewController in order to get full control. Then you can update UIPageControl property programmatically upon updating your Page. Have a look at this article.

1

There is a workaround get the subviews of UIPageViewController, set the value to currentPage.

for subView in self.view.subviews{
     if let pageControl = subView as? UIPageControl,
         pageControl.numberOfPages >  currentIndex{
         pageControl.currentPage = currentIndex
     }
}
Ramesh R C
  • 644
  • 7
  • 17
0

You can, all you need to do is maintain the page index in a variable, let's call it currentPageIndex and use the following method:


// Part of UIPageViewControllerDelegate 

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
  return currentPageIndex;
}

// In your Button action, set this variable

@IBAction func nextPage(_ sender: Any) {
   var index = (pageViewController?.viewControllers?.last as! SinglePageViewController).pageIndex
   currentPageIndex = index
}

That's it!! Your page indicator should work now. I was stuck in a same situation as yours.

nayan dhabarde
  • 1,734
  • 1
  • 19
  • 38