6

I've been making a sample app with scrolling between UIViewControllers but the point is i want to disable the bouncing effect at the end of scrolling and when going back to the first UIViewController.

here is my code :

class PageViewController: UIPageViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate{

    var index = 0
    var identifiers: NSArray = ["FirstNavigationController", "SecondNavigationController"]
    override func viewDidLoad() {

        self.dataSource = self
        self.delegate = self

        let startingViewController = self.viewControllerAtIndex(self.index)

        let viewControllers: NSArray = [startingViewController]
        self.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    func viewControllerAtIndex(index: Int) -> UINavigationController! {

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        //first view controller = firstViewControllers navigation controller
        if index == 0 {

            return storyBoard.instantiateViewControllerWithIdentifier("FirstNavigationController") as! UINavigationController

        }

        //second view controller = secondViewController's navigation controller
        if index == 1 {

            return storyBoard.instantiateViewControllerWithIdentifier("SecondNavigationController") as! UINavigationController
        }

        return nil
    }
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier!)

        //if the index is the end of the array, return nil since we dont want a view controller after the last one
        if index == identifiers.count - 1 {

            return nil
        }

        //increment the index to get the viewController after the current index
        self.index = self.index + 1
        return self.viewControllerAtIndex(self.index)

    }

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

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier!)

        //if the index is 0, return nil since we dont want a view controller before the first one
        if index == 0 {

            return nil
        }

        //decrement the index to get the viewController before the current one
        self.index = self.index - 1
        return self.viewControllerAtIndex(self.index)

    }

}

Take a look here :

enter image description here

AaoIi
  • 8,288
  • 6
  • 45
  • 87

1 Answers1

7

I don't know if you can. UIPageController is not very customizable.

Personally, when I want to scroll between UIViewController, I prefer using a simple UIViewController, which will be a container, with an UIScrollView in it. Then I add programmatically all the controllers in the contentSize of the UIScrollView. You have to add all the controllers as child of the container.

UPDATE iOS 9

func setupDetailViewControllers() {
    var previousController: UIViewController?
    for controller in self.controllers {
        addChildViewController(controller)
        addControllerInContentView(controller, previousController: previousController)
        controller.didMoveToParentViewController(self)
        previousController = controller
    }
}

func addControllerInContentView(controller: UIViewController, previousController: UIViewController?) {
    contentView.addSubview(controller.view)
    controller.view.translatesAutoresizingMaskIntoConstraints = false

    // top
    controller.view.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true

    // bottom
    controller.view.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true

    // trailing
    trailingContentViewConstraint?.active = false
    trailingContentViewConstraint = controller.view.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor)
    trailingContentViewConstraint?.active = true

    // leading
    let leadingAnchor = previousController?.view.trailingAnchor ?? contentView.leadingAnchor
    controller.view.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true

    // width
    controller.view.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true
}

PREVIOUS ANSWER

Like so :

scrollView is an IBOutlet with contraints to each edge of the ContainerViewController

class ContainerViewController: UIViewController {

@IBOutlet var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.bounces = false
    scrollView.pagingEnabled = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

override func viewDidLayoutSubviews() {
    initScrollView()
}

func initScrollView(){
    let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1

    viewController1.willMoveToParentViewController(self)
    viewController1.view.frame = scrollView.bounds

    let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2

    viewController2.willMoveToParentViewController(self)
    viewController2.view.frame.size = scrollView.frame.size
    viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0)

    scrollView.contentSize = CGSize(width: 2 * scrollView.frame.width, height: scrollView.frame.height)

    scrollView.addSubview(viewController2.view)
    self.addChildViewController(viewController2)
    viewController2.didMoveToParentViewController(self)

    scrollView.addSubview(viewController1.view)
    self.addChildViewController(viewController1)
    viewController1.didMoveToParentViewController(self)
}}
GaétanZ
  • 4,870
  • 1
  • 23
  • 30
  • is there is any other possible solution without using `UIScrollView` instead of `UIPageViewController` ? or how is it possible to allow only horizontal scrolling ? – AaoIi Jun 22 '15 at 08:20
  • Your scrollView should not scroll vertically since the height of your contentSize is equal to the height of your scrollView. – GaétanZ Jun 22 '15 at 10:12
  • in your way how can i check that I'm now in viewController2 ? – AaoIi Jun 22 '15 at 10:39
  • I haven't my computer but you can use index as you did in the containerViewController. You can set the containerViewController as the delegate of the scrollView and change the index each time the content moves. Or you can change it from your UINavigationViewController in viewDidAppear using the parentViewController property. – GaétanZ Jun 22 '15 at 10:53
  • So if i need to add 1 more viewController how can i make it work (the equation )? i do width: 3 * ... ? – AaoIi Jun 22 '15 at 12:52
  • Like the others. You instantiate it, prepare it to become a child, make the contentSize of the scrollView bigger, add its view to the contentSize of the scrollView. The only difference from viewController2 will be its origin in the contentSize : CGPoint(x: view.frame.width * 2, y: 0) – GaétanZ Jun 22 '15 at 13:02
  • And sure: scrollView.contentSize = CGSize(width: 3 * scrollView.frame.width, height: scrollView.frame.height) – GaétanZ Jun 22 '15 at 13:14
  • Seems some part of code are missing.. – Jayprakash Dubey Oct 08 '18 at 04:15