Search code examples
iosswiftuipageviewcontroller

Updating the number of VC's in a UIPageViewController does not update the page display


I have a class that implements UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource. I have three VC's at the start of the app. The page display at the bottom has three dots in it, one is filled in for the selected vc:

O o o

Where O is filled and o's are not. I add another vc to the list of vc's I'd like to page between. I call setViewControllers which in turn forces calls to presentationCount to fire (returning the new value).

Here's what is odd. I can swipe between all of the vc's including the new one, but the page display at the bottom still shows 3 dots:

o o O

If I tap on the page display the bottom (not to move to a page, but just the middle of the page display. It expands and shows all four dots:

o o O o

It operates fine the whole time, but the page display doesn't update. Anyone know what call is needed to update the page display at the bottom?


Solution

  • Kinda of quirky...

    To trigger the call to presentationCount(...), we need to "toggle" the .delegate off and back on.

    To get the pageControl (the dots) to update, we want to "force" a UI layout pass.

    So, in your code to add another Page VC (or if you're also removing them):

    // instantiate and add a new "page" VC
    let newVC = TestPageVC()
        
    myPages.append(newVC)
        
    // toggle the delegate off/on
    self.delegate = nil
    self.delegate = self
        
    // this will "re-load" the page control
    self.view.setNeedsLayout()
    self.view.layoutIfNeeded()