title: UIPageViewController not completing transition on button click
body: <p>I have a <code>UIPageViewController</code> that handles an array of <code>viewControllers</code>
  when I swipe between them everything is fine, But I have a few methods (after successful login and after menu button click) that trigger a 'jump' to a <code>viewController</code> in the <code>UIPageViewControllers</code> array.
  whenever I jump to a page, the transition isn't complete and I have a screen that is 'stuck' between two <code>viewControllers</code></p>
  
  <p>For simplicity, I will only add the successful login code, because I set everything the same for button clicks, and the same thing happens.</p>
  
  <p>This is my UIPageViewController setup:</p>
  
  <pre><code>import UIKit
  
  class PageViewController: UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate , ClubViewControllerDelegate , CustomSvDelegate{
  
  //clubvcdelegate protocol method
  
  func fbLoggedInWithSuccess(){
  self.displayPageForIndex(index: 4, animated: false)
  }
  
  //menubuttondelegate protocol method
  func menuButtonPressed0() {
  
      self.displayPageForIndex(index: 0, animated: false)
      self.currentPageIndex = 0
      self.closeMenu()
  }
  
  //page swipe control
  
  lazy var vcArray : [UIViewController] = {
      return [self.vcInstance (name : "clubVC"),
              self.vcInstance (name : "menuVC"),
              self.vcInstance (name : "contactVC"),
              self.vcInstance (name : "aboutVC"),
              self.vcInstance (name : "membersVC")]
      }()
  
  private func vcInstance(name : String) -&gt; UIViewController{
      let storyBoard = UIStoryboard(name: "Main", bundle: nil)
      return storyBoard.instantiateViewController(withIdentifier: name)
  }
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
      self.dataSource = self
      self.delegate = self
      if let clubVC = vcArray.first {
          let club = clubVC as! ClubViewController
          club.delegate = self
          setViewControllers([clubVC], direction: .forward, animated: true, completion: nil)
      }
      self.generateStackView()
  }
  
  
  override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      for view in self.view.subviews {
          if view is UIScrollView {view.frame = UIScreen.main.bounds} else if view is UIPageControl {view.backgroundColor = UIColor.clear}
      }
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -&gt; UIViewController?{
      guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}
  
      let previousIndex = viewControllerIndex - 1
  
      guard previousIndex &gt;= 0 else {return vcArray.last}
  
      guard vcArray.count &gt; previousIndex else {return nil}
  
      return vcArray[previousIndex]
  
  }
  
  
  public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -&gt; UIViewController?{
      guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}
  
      let nextIndex = viewControllerIndex + 1
  
      guard nextIndex &lt; vcArray.count else {return vcArray.first}
  
      guard vcArray.count &gt; nextIndex else {return nil}
  
      return vcArray[nextIndex]
  
  }
  
  public func presentationCount(for pageViewController: UIPageViewController) -&gt; Int{
      return vcArray.count
  }
  
  
  public func presentationIndex(for pageViewController: UIPageViewController) -&gt; Int{
      guard let firstViewController = viewControllers?.first ,
      let firstViewControllerIndex = vcArray.index(of: firstViewController) else {
          return 0
      }
      return firstViewControllerIndex
  }
  </code></pre>
  
  <p>and this is how I set up things where fblogin is successful:</p>
  
  <pre><code>protocol ClubViewControllerDelegate:class {
  func fbLoggedInWithSuccess()
  }
  
  class ClubViewController: UIViewController , FBSDKLoginButtonDelegate {
  var delegate:ClubViewControllerDelegate?
  
  func showFbDetails(){
  
      let accesToken = FBSDKAccessToken.current()
  
      guard let accesTokenString = accesToken?.tokenString else {return}
  
      let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accesTokenString)
  
      FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
          if error != nil {
              print ("something went wrong" , error!)
              return
          }
          print ("succes")
          self.delegate?.fbLoggedInWithSuccess()
      })
  
  }
  </code></pre>
  
  <p>Pictures:
  <a href="https://i.stack.imgur.com/lB9jN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lB9jN.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/uf66I.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uf66I.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT2</strong>
  This only happens when animated it true, when false it completes correctly</p>
  
  <p><strong>EDIT3</strong>
  This is the method I now use to set view controllers : </p>
  
  <pre><code>    func displayPageForIndex(index : Int , animated : Bool = true){
      assert(index &gt;= 0 &amp;&amp; index &lt; self.vcArray.count, "Error: Attempting to display a page for an out of bounds index")
      if self.currentPageIndex == index {return}
      if index &lt; self.currentPageIndex {
          self.setViewControllers([self.vcArray[index]], direction: .reverse, animated: false, completion: nil)
      } else if index &gt; self.currentPageIndex {
          self.setViewControllers([self.vcArray[index]], direction: .forward, animated: false, completion: nil)
      }
  }
  </code></pre>
  
  <p>I call it inside of <code>fbLoggenInWithSuccess</code> and <code>menuButtonPressed</code>.
  Also, have a variable CurrentIndex that holds the current index of the page</p>