I've solved for my case in a somewhat of hack. For each ContentView
, I have a UIImageView
within a UIScrollView
for zooming. My problem was that upon booting up the app, if the user zoomed before swiping, going to the next page while zoomed in wouldn't work too well. I use the following code (Swift 1.2) to solve this problem. As I've said, it's a bit of a hack though.
var layoutsubs = false
override func viewDidLoad() {
super.viewDidLoad()
//Other code for implementing pageViewController omitted
//add pageViewController to main view
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
pageViewController.didMoveToParentViewController(self)
//Load to the viewController after the starting VC, then go back to the starting VC
var viewControllers = [afterVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
viewControllers = [startingVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Reverse, animated: true, completion: nil)
}
override func viewWillLayoutSubviews() {
//Load the viewController before the starting VC then go back to the starting VC
//viewWillLayoutSubviews() is called multiple times, so do this only once
if !layoutsubs {
let startingVC = self.viewControllerAtIndex(imageIndex) as ContentViewController
let beforeVC = pageViewController(pageViewController, viewControllerBeforeViewController: startingVC) as! ContentViewController
var viewControllers = [beforeVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Reverse, animated: true, completion: nil)
viewControllers = [startingVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
layoutsubs = true
}
}
Essentially, I load the view controllers before and after the starting view controller. I do this by setting each one to the VC to see via setViewControllers(_:direction:animated:completion:)
(see ref), then by going back to the starting view controller. Why is this in two different functions? Well, if you put it all in one, only one of the two view controllers next to the starting VC will load. This may be desirable for some cases, but I needed all three VCs (before, starting, and after) to load.
I'm not sure how well this method would work if the UIPageViewController
was already loaded. For instance, if you needed to load the page 2 away from the page being viewed, after a few swipes. It may skip around if you put it in willTransitionToViewControllers()
.