Search code examples
iosswiftuiviewcontrolleruipageviewcontroller

Changing Initial View Controller Shown in UIPageViewController


I just started programming with swift a few months ago and I am currently working on a few random projects to help me learn the language better. Currently, I am trying to create a UI similar to Snapchat's by using a UIPageViewController. The horizontal scroll is working and I can navigate through the pages correctly but I want to change the initial view controller that is presented. In my code below, I have three ViewControllers (vc1, vc2, vc3). I want to keep them in that same order but display vc2 first, so basically have it in the middle of the other two. All help is appreciated. Thanks!

import UIKit
import FirebaseDatabase
import FirebaseAuth
import Firebase

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource {

    let Cards = CardsVC()

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "MessagesVC")
        let vc2 = sb.instantiateViewController(withIdentifier: "CardsVC")
        let vc3 = sb.instantiateViewController(withIdentifier: "ProfileVC")

        return[vc1, vc2, vc3]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        //if Auth.auth().currentUser?.uid == nil{
            //logout()
        //}else{
        self.dataSource = self

        if let firstViewController = viewControllerList.first{
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
        }
    //}
    }

    func logout(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let verifyPhoneVC = storyboard.instantiateViewController(withIdentifier: "enterPhoneNumber")
        present(verifyPhoneVC, animated: true, completion: nil)

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

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else {return nil}
        guard viewControllerList.count > previousIndex else {return nil}

        return viewControllerList[previousIndex]
    }

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

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
        let nextIndex = vcIndex + 1
        guard viewControllerList.count != nextIndex else {return nil}
        guard viewControllerList.count > nextIndex else {return nil}

        return viewControllerList[nextIndex]
    }

}

Solution

  • You have the entire code ready, all that you need to do is pass the secondViewController from the viewControllerList in viewDidLoad thats all

    So replace

    if let firstViewController = viewControllerList.first{
                self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
       }
    

    with

    let secondViewController = viewControllerList[1]
        self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
    

    Hope this helps