Multiple UIViewControllers inside master UIViewController in Swift - swift

I am building a game in Swift 3 for tvOS. The game has a CPU intensive game background that includes several animations. This background is a UIViewController dedicated to this task. I would like to use this background in all other UIViewControllers.
When navigating between the ViewControllers the background animation need to continue smoothly. What would be the best way to do this?
Schematic example of the shared background UIViewController:

Use parent child view controller containment.
Create a parent view controller. Have your constant animation run in that view controller's content view.
Add a container view to that view controller and an embed segue, linked to the first child view controller you want to go inside the container view.
Then use the view controller child view controller support methods and transition methods to swap the child view controller for another child view controller. (See transition(from:to:duration:options:animations:completion:). Also see the section "Implementing a Container View Controller" in the UIViewController Class description in the Xcode docs. In particular, this bit:
Here are the essential methods you might need to call:
addChildViewController(_:)
removeFromParentViewController()
willMove(toParentViewController:)
didMove(toParentViewController:)

Related

How do I put two UIViewController vertically in UIKit?

I want to put two UIViewController in one screen simultaneously in UIKit. However, I couldn't find the solution.
Like this:
I want to display both UINavigationController and Admob Banner in one screen and independently. The one doesn't affect the other.
Could you tell me how to do this?
You want to use view controller containement. The easiest way to do this is as follows.
Open your storyboard.
Add a view controller. Let's call it "Parent". Give it a unique identifier.
Tap the "+" to add a new component to your parent view controller. Search on "Container" and drag 2 container views onto your parent view controller. Those container views will contain the content view of your 2 view controllers. Set up the constraints on those container views so they are laid out top and bottom as you show in your picture.
Next add view controllers for view controller 1 and view controller 2 to your storyboard. (you can also add links to view controllers from other storyboards, but I'll ignore that.) Lets call those Child1 and Child2.
Control-drag from each container in Parent onto the child view controller you want to appear in that container. When prompted, select that you want to create an "embed segue".
An embed segue tells your app that it should load the child view controllers when Parent is loaded, and make their content views subviews of the container views.
In Parent, the PrepareForSegue() method will fire as each child view controller has been initialized and its view is getting ready to be loaded. At that point you can pass information to the child view controllers, set up delegate links, etc. (Note that you can't, and shouldn't, manipulate the child view controller's view hierarchies directly. Instead, pass data to the child view controllers using proprerties or methods of those view controllers, and have the children's viewDidLoad methods install that data into the views as needed.

create a viewcontroller below other viewcontroller

I want to create an ios app in which I want to flip uiviewcontrollers. I want to use swipe gesture to change between uiviewcontrollers.
I have 2 uiviewcontrollers, in which I want to set one view controller to the top of uiviewcontroller cover and one view controller to the bottom ,and then make an animation that let a top uiviewcontroller slide to left to out of screen and the bottom uiviewcontroller will appear step by step.
What is the Best way to do this ?
Create a RootViewController with container views.
Add the child view controllers to the root view controller.
Add gesture recognizers to manipulate the views.
Use the "view controller containement" a feature of iOS5, to make managing the lifecycle calls easier.
As Jasper wrote you should create new UIViewController - "root" which will hold the child UIViewControllers- "bottom" and "top". It will also display their views. To achieve an effect that "bottom" is underneath, you should add its view as a subview first.
Take a look at http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html It might give you an idea how to handle with child UIViewControllers

positioning UIViewController containment children

I was reading the documentation:
You need to decide how many children can be displayed by your view
controller at once, when those children are displayed, and where
they appear in your view controller’s view hierarchy.
But in which method should I position the view controller children's view? Say I have two UIViewController in the container and I want one next to the other.. how do I do this?
In one of my articles I demonstrated how to create a simple dashboard app using UIViewController Containment.
http://www.highoncoding.com/Articles/848_Creating_iPad_Dashboard_Using_UIViewController_Containment.aspx
Each of the child view controllers has a view property. You can set the frame of those views when you add them to your own view.
It depends on the context of the situation that you may have. If you need to display all of the children when displaying the view for the first time, then add the view controllers and views in viewDidLoad ( if using a xib or nib) or in loadView ( if done programatically). If you need to show the child view controllers on demand, like after the touch of button, then you can add the child view controller and associated view in a separate method.
You will need to layout the views of the child view controllers as you would layout any other subviews. Remember view controller containment is just another ways of allowing you to modularize your code.
Check out this link which helps explain how to add the child view controllers: Animate change of view controllers without using navigation controller stack, subviews or modal controllers?
Here is a simple sample project that shows how to add child view controllers:https://github.com/toolmanGitHub/stackedViewControllers
Good Luck!

addChildViewController and presentViewController

iOS 5 introduces the concept of custom container view controller and provides API like addChildViewController. Question: can you add a view controller as a child and still present it using presentViewController? Does doing the latter automatically make it a child view controller of the presentingViewController?
That's not how it's supposed to be used.
The parent/child relationship is for when a view controller has subviews that are managed by their own view controllers, for example a UITabBarController, where the parent view controller draws the tabs and the child view controllers draw the content of each tab.
If you present a view controller using presentViewController, it generally takes over the whole screen, or appears in a modal so that the presenting view controller is no longer in control. In that scenario there's no reason for the presenter to be the parent because it doesn't need to cooperate with the presented controller - it just gets out of the way until the presented controller is dismissed again.
Why is it that you wanted to do this? If it's just so that the view controllers have a reference to one another and can pass data, there are other ways to do this (e.g. the delegate pattern, NSNotifications, or even just a property linking the two).

iPhone - nested views & controllers

Is it possible to have a single iPhone screen with its view loaded from a xib by that screen's UIViewController, but then another UIView within that screen with content loaded from a separate xib file? If so, is it possible to have that nested view's events handled by a separate custom UIViewController subclass from the rest of the screen? If both of these things are possible, are they also advisable?
It is possible. Apple suggests against having more than one UIViewController active on screen at once, so they would advise against. I would suggest only doing it if the reason for the second view controller is navigation or modal.
A view controller with the purpose of loading other view controllers, like a navigation controller, needs some screen space for itself and uses the rest to load another view controller. That is fine. The criteria here is that only one controller is presenting content while the other is presenting navigation.
A view controller could load another view controller to perform some limited task like selecting an item from a list or entering some text. The second view controller might only fill part of the screen. The criteria here is that the one controller behaves modally and will only be displayed long enough to get some user input.
As for the general case of splitting the screen between two view controllers that are presenting content, the Apple suggestion is that you have a single class derived from UIViewController manage the views. If the view is complex enough to warrant other controllers, then derive them from NSObject and have the master view controller manage the child controllers along with the views. The child controllers would have the master controller as a delegate, and the master controller would pass views to the child controllers to manage but not own.