title: iOS UIPageViewController Gets Confused and ViewControllers Are Not Displayed
body: <p>We have left and right buttons set up for the user to page through different cars quickly. Our Page View Controller loses the view controller if the user taps quickly to the next page 10 or more times. </p>
  
  <p>Here is the vehicle page with the car showing correctly (blurred to hide non-relevant information). See image here:</p>
  
  <p><a href="https://i.stack.imgur.com/fg2AP.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/fg2AP.jpg" alt="Shows Vehicle Correctly"></a></p>
  
  <p>If scrolling animation is on (true), it loses the vehicle page after tapping the right arrow 6 or more times quickly. See image here:</p>
  
  <p><a href="https://i.stack.imgur.com/bo279.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/bo279.jpg" alt="enter image description here"></a></p>
  
  <p>Code:</p>
  
  <pre><code>private func show(viewController:UIViewController, going direction: UIPageViewControllerNavigationDirection) {
      let viewControllers = [viewController]
      let isAnimated = true // false always works. However, animation is required.
      setViewControllers(viewControllers, direction: direction, animated: isAnimated, completion: nil)
  }
  </code></pre>
  
  <p>While debugging and when the page view controller has stopped showing the cars, I ensured that the view controller being set is not nil and the listing (car) is also non-nil.</p>
  
  <p>I tried a variant of the solution from <a href="https://stackoverflow.com/questions/13633059/uipageviewcontroller-how-do-i-correctly-jump-to-a-specific-page-without-messing">UIPageViewController, how do I correctly jump to a specific page without messing up the order specified by the data source?</a> where the completion block is used. However, it did not work.  </p>
  
  <pre><code>weak var pvcw: UIPageViewController? = self
  setViewControllers(viewControllers, direction: direction, animated: true, completion: {(_ finished: Bool) -&gt; Void in
      let pvcs: UIPageViewController? = pvcw
      if pvcs == nil {
          return
      }
      DispatchQueue.main.async(execute: {() -&gt; Void in
          pvcs?.setViewControllers(viewControllers, direction: direction, animated: false) {(_ finished: Bool) -&gt; Void in }
      })
  })
  </code></pre>
  
  <p>Any ideas? Thank you.</p>
  
  <h1>Update</h1>
  
  <p>I noticed that sometimes the contained View Controller can be off centered as opposed to entirely missing.</p>
  
  <p><a href="https://i.stack.imgur.com/239qR.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/239qR.jpg" alt="View Controller Off Centered"></a></p>
  
  <p>I looked deeper into the scenario of the view controller missing entirely. Clicking on "Debug View Hierarchy" and turning on "<a href="https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/DebuggingTricksandTips.html" rel="noreferrer">Show Clipped Content</a>" revealed the following when the View Controller is missing entirely:</p>
  
  <p><a href="https://i.stack.imgur.com/quAne.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/quAne.jpg" alt="Turning on Show Clipped Content"></a></p>
  
  <p><a href="https://i.stack.imgur.com/cipQn.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/cipQn.jpg" alt="Clipped Content"></a></p>
  
  <p>So, it seems the missing content is clipped / out of bounds. </p>
  
  <p>Showing only the wireframes reveals the following:</p>
  
  <p><a href="https://i.stack.imgur.com/vJ6h4.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/vJ6h4.jpg" alt="wire frames only"></a>    </p>
  
  <p>The Page View Controller has a </p>
  
  <ul>
  <li>_UIPageViewControllerContentView that contains a </li>
  <li>_UIQueuingScrollView that contains a </li>
  <li>UIView that contains a</li>
  <li>VehicleDetailTableViewController (the UITableViewController with a car image and details).</li>
  </ul>
  
  <p>I also see the _UIQueuingScrollView's bounds is quite different when things are weird. The bounds have an x of 1125 as opposed to an X of 375 when everything is normal. </p>
  
  <p>This only happens when using a Transition Style of scroll as opposed to Page Curl. When using Page Curl, things work fine. </p>
  
  <p><strong>How can we prevent / fix this?</strong></p>
  
  <h1>Second Update</h1>
  
  <p>This code makes the problem go away. However, it leaves a more jarring experience. Perhaps due to the delay of 0.4 seconds, the blue background shows sometimes in normal use.</p>
  
  <pre><code>private func show(viewController:UIViewController, going direction: UIPageViewControllerNavigationDirection) {
  
      let viewControllers = [viewController]
      setViewControllers(viewControllers, direction: direction, animated: true, completion: { (_) in
  
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.4, execute: {
              self.setViewControllers(viewControllers, direction: direction, animated: false, completion: nil)
          })
      })
  }
  </code></pre>
  
  <p>This is not a good user experience. <strong>Is there a better approach?</strong></p>
  
  <p>I want the scroll transitions to be smooth, not briefly show the blue background, and to not lose its content aka View Controller content.</p>