I am creating a scrollView that is able to display ViewController views one after the other. This is the code I implemented:
scrollView.contentSize = CGSize(width: screenWidth * 3, height: screenHeight)
let firstVC = FirstViewController()
let secondVC = SecondViewController()
let thirdVC = ThirdViewController()
self.addChild(firstVC)
self.scrollView.addSubview(firstVC.view)
firstVC.willMove(toParent: self)
self.addChild(secondVC)
self.scrollView.addSubview(secondVC.view)
secondVC.willMove(toParent: self)
self.addChild(thirdVC)
self.scrollView.addSubview(thirdVC.view)
thirdVC.willMove(toParent: self)
firstVC.view.frame.origin = CGPoint.zero
secondVC.view.frame.origin = CGPoint(x: screenWidth, y: 0)
thirdVC.view.frame.origin = CGPoint(x: screenWidth*2, y: 0)
view.addSubview(scrollView)
scrollView.fillSuperview()
I wanted to know if it was possible to call each ViewController lifecycle method whenever I'm scrolling through them.
So for example when I'm passing from vc1 to vc2 I want that:
vc1 fires viewwillDisappear method
vc2 fires viewWillAppear method
The easiest solution is to use a page view controller. When you do that, the appearance methods for the children will be called for you automatically (and it gets you out of all of that dense code to manually populate and configure the scroll view):
If you really want to use the scroll view approach, don't do the view controller containment code up front, but rather only add them as they scroll into view (and remove them when they scroll out of view). You just need to set a delegate for your scroll view and implement a
UIScrollViewDelegate
method.So, for example, I might only populate my scroll view with container subviews for these three child view controllers. (Note
containerViews
in my example below are just blankUIView
instances, laid out where the child view controller views will eventually go.) Then I can see if theCGRect
of the visible portion of the scroll view intersects with a container view, and do the view controller containment in a just-in-time manner.In both of these scenarios, you will receive the containment calls as the views appear.