UIPageViewController not showing anything

I have a PageContainerViewController ( a regular view controller with nothing in it ) that I Show Detail present from a previous view controller, which is supposed to embed a UIPageViewController in itself. The UIPageViewController is supposed to show, right now, two View Controllers, but it doesn’t. The PageContainerViewController shows up as a blank white screen, and the UIPageViewController seems to be nowhere (I changed the background color to a bright green and still all I see is white).

 

Here’s my code in PageContainerViewController (which has the UIPageViewControllerDataSource):

     var pageViewController: UIPageViewController?
    
    lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVc(viewController: "vc1"),
                self.newVc(viewController: "vc2")]
    }()
    
    var currentlyShowingIndex = 0
    
    func newVc(viewController: String) -> UIViewController {
        return storyboard!.instantiateViewController(withIdentifier: viewController)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)ntroller
        
        pageViewController?.dataSource = self
        
        let arr: [UIViewController] = [orderedViewControllers.first!]
        
        pageViewController?.setViewControllers(arr, direction: .forward, animated: false, completion: nil)
        
        pageViewController?.view.frame = self.view.bounds
        
        self.addChildViewController(self.pageViewController!)
        self.view.addSubview(self.pageViewController!.view)
        pageViewController?.didMove(toParentViewController: self)
        
        pageViewController?.view.backgroundColor = UIColor.green
        
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }
        
        let previousIndex = viewControllerIndex - 1
        
        guard previousIndex >= 0 else {
            return nil
        }
        
        guard orderedViewControllers.count > previousIndex else {
            return nil
        }
        
        currentlyShowingIndex -= 1
        
        return orderedViewControllers[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }
        
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count
        
        guard orderedViewControllersCount != nextIndex else {
            return nil
        }
        
        guard orderedViewControllersCount > nextIndex else {
            return nil
        }
        
        currentlyShowingIndex += 1
        
        return orderedViewControllers[nextIndex]
    }
    
    func viewController(at index: Int) -> UIViewController {
        return orderedViewControllers[index]
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return currentlyShowingIndex
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }

Powered by WPeMatico

About

You may also like...

Comments are closed.