Search code examples
iosswift4viewcontrolleruipageviewcontroller

how to have a page view controller with more than one page But using just one view controller in swift 4?


I have a page view controller that working well - I want having one view controller But in page view controller having more than one page - I mean when user scroll can see that view controller again and in story board there is Just one view controller here is my codes that working But when I want to change the number of the pages to more than one pages it doesn't work properly here is my codes

class mainPage:  UIPageViewController, UIPageViewControllerDataSource , UIPageViewControllerDelegate {


    let pageVC3 = UIPageControl()
    lazy var VCArr : [UIViewController] = {

        return [self.VCInstance(name : "page")]

    }()

    private func VCInstance(name : String) -> UIViewController {

        return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)


    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let pageControl = UIPageControl.appearance()

        pageControl.layer.position.y = self.view.frame.height - 20

        pageControl.pageIndicatorTintColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)

        pageControl.currentPageIndicatorTintColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.6)

        //Setting the background of the view controller so the dots wont be on a black background

        self.view.backgroundColor = UIColor.black
        self.dataSource = self
        self.delegate = self
        if let firstVC = VCArr.first {

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

        }

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        for subV in self.view.subviews {
            if type(of: subV).description() == "UIPageControl" {

                let pos = CGPoint(x: 0, y: 600)
                subV.frame = CGRect(origin: pos, size: subV.frame.size)

            }
        }

        for view in self.view.subviews {
            if view is UIScrollView {
                view.frame = UIScreen.main.bounds
            }else if view is UIPageControl{
                view.backgroundColor = UIColor.clear

            }
        }
    }



    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
        guard let viewControllerIndex = VCArr.index(of: viewController) else {
            return nil

        }

        let previousIndex = viewControllerIndex-1
        guard previousIndex >= 0  else {
            return nil

        }

        guard VCArr.count > previousIndex else {
            return nil
        }

        return VCArr[previousIndex]
    }

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
        guard let viewControllerIndex = VCArr.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex+1
        guard nextIndex < VCArr.count
            else {
                return nil
        }

        guard VCArr.count > nextIndex else {
            return nil
        }

        return VCArr[nextIndex]
    }





    public func presentationCount(for pageViewController: UIPageViewController) -> Int{
        return 3
    }


    public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
        guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
            return 0
        }


        return firstViewControllerIndex
    }


  }

Solution

  • so here is the answer that I found

    I had to use this

     lazy var VCArr : [UIViewController] = {
    
            return []
    
        }()
    

    and then using this

     var numberOfPages = 3
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            for _ in 0...numberOfPages - 1 {
                VCArr.append(self.VCInstance(name : "page"))                
            }
       }
    

    so as you see the page view controller pages can be changed by the number of the pages