Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIPageViewController in present app

I have an app in working state in which I have a three screens 1,2,3 each of that screen is associated with a UICollectionView which are created programatically now I want to modify the current implementation & add those collection views to UIPageViewControllers.

I tried to find many tutorials related to PageViewController with CollectionViews but was not able to find anything. Can anyone help me out in implementing this or can give me a reference related to this.

I have also referred this tutorial, but hard luck for me :(

like image 206
iYoung Avatar asked Jul 08 '15 10:07

iYoung


2 Answers

Just follow some of those page view controller tutorials, and when you get to the point of instantiating the child view controllers, make those children collection view controllers. Just as in your example tutorial where it is using UIImageView try replacing it with UICollectionViews & you will achieve what you want.

Do tell if you face any difficulties. Happy Coding!

like image 108
Mac Keeda Avatar answered Sep 23 '22 21:09

Mac Keeda


You can do this by:

1) Subclassing UIPageViewController and conforming to the UIPageViewControllerDataSource protocol. The data source will tell the pageViewController which view controllers will come next or before the current view controller being presented.

2) In viewDidLoad(), set the dataSource as self so the delegate methods get called. Also, call self.setViewControllers([collectionViewController], direction: .Forward, animated: true, completion: nil). collectionViewController will be an array populated with 1 collection view controller (the first view controller presented).

Example:

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = self   //so our delegate methods get called

    //use tags to reference each controller
    let collectionViewOne = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
    collectionViewOne.view.tag = 0
    let collectionViewTwo = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
    collectionViewTwo.view.tag = 1
    let collectionViewThree = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
    collectionViewThree.view.tag = 2

    collectionViewControllers = [collectionViewOne, collectionViewTwo, collectionViewThree]

    self.setViewControllers([collectionViewOne], direction: .Forward, animated: true, completion: nil)
}

3) Implement the UIPageViewControllerDataSource methods:

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var index = viewController.view.tag

    if index == 0 {
        return nil
    }

    if index > 0 {
        index--
    }
    return collectionViewControllers[index] as? TestCollectionViewController
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    var index = viewController.view.tag

    if index == 2 {
        return nil
    }

    if index < collectionViewControllers.count - 1 {
        index++
    }
    return collectionViewControllers[index] as? TestCollectionViewController
}

We return nil if the index will present an out of bounds view controller

Source code of TestCollectionViewController and the UIPageViewController subclass can be found here

like image 34
BenJammin Avatar answered Sep 25 '22 21:09

BenJammin