Put two UIViewControllers on screen side by side - ios

On iPad, I've got a main UIViewController that's supposed to embed side by side two childViewControllers. How can I instantiate them both, and put them on screen?

You can use container views in IB. Drag out 2 container views into the controllers view, and size them how you want. You will automatically get two connected view controllers, sized appropriately. The main view controller, and its two children will all be instantiated at the same time. If you need to get access to the children from the parent, you use self.childViewControllers.

Sounds like you should use a UISplitViewController as a container for two view controllers:
The UISplitViewController class is a container view controller that
manages the presentation of two side-by-side view controllers.

Related

How to implement multiple views in single parent view in iPad?

I have an iOS application which runs on the iPad. So the iPad has a larger screen than iPhones so that it can include many views within a single UIViewController which makes the UIViewController code very bulky and design is messed up.
I've been using ContainerView for now. I use 'protocol delegate' to pass data from child view's view controller to container view's view controller and NotificationCenter to pass data from container view's view controller to child view's view controller. I would like to know other approaches to this.

What are the benefits of using a UINavigationController over adding a view with addSubView?

Let's say I have two pages with two UIViewControllers, UIViewController1 & UIViewController2.
If I want to show a UIViewController2 on top of UIViewController1 I have three options:
using UINavigationController pushViewController.
using presentViewController.
addSubView : UIViewController1.view.addSubView(UIViewController2.view)
If I need to a transition between my views, I prefer the third option because it gives me much more control over the views.
Is there any difference between these three options in terms of performance?
Before iOS 6 you were not supposed to do option 3. View controllers were meant to control the entire screen. In iOS 6 Apple added support for parent and child view controllers. You could make another view controller your child and add it's content views to yours.
If you are going to use option 3 then that's what you need to do. If you don't you will have a variety of problems.
There is even support for parent/child view controller built into storyboards. You can add a container view to a view controller, and then control drag from the container view onto another view controller's scene. When you do that the system creates an "embed segue" that sets up the child view controller inside the container view and wires up the parent/child relationship for you.
Your first 2 options are for when you want the new view controller to replace, or at least cover, the first view controller. Option 3 is for when you want a region of your screen to be managed by another view controller.
Option 3 (using a child view controller) does mean that both view controllers will be active and in memory at the same time, so you can't release the covered view controller's data storage while it's inactive like you can in a push or modal presentation. However unless your view controllers hold and present huge data structures this isn't much of a concern. In both a push and a modal presentation the covered view controller sticks around in memory anyway, waiting to be uncovered. You have to take special steps in order to free any memory while a view controller's view is covered and then reallocate it when it is displayed again - something that is unusual.
Just for two view controllers, it will not create major difference. UINavigationController is mainly used to maintain navigation stack.
But as you are having just two view controllers, you can use alternate way also.
If you are looking for transitions with NavigationController, you can use UIView Transitions for customisation of push pop animation.
Please refer following links for UIView Transitions
https://www.objc.io/issues/12-animations/custom-container-view-controller-transitions/
http://www.raywenderlich.com/86521/how-to-make-a-view-controller-transition-animation-like-in-the-ping-app
https://www.captechconsulting.com/blogs/ios-7-tutorial-series-custom-navigation-transitions--more
http://applidium.github.io/ADTransitionController/

Container controller view sizing

I'm currently building a custom view controller container for my iOS project and I nearly understand what I'm doing. The one thing I'm stuck on is how do you (properly - aka not a hack) add a view controller in PART of the Parent controller's frame. See how there are multiple view controllers/views in the email app? How does one build a custom controller container that designates the location of such sub-view controllers? How do you properly add such a controller? I'd like to know the "correct" way as designated by apple (best practice).
EDIT: After looking at this some more I was thinking a possible way would be to create views with custom sizing and then push those to the parent. Is this the correct way?
You sort of answered it yourself. The view you have as an example uses a UISplitViewController to show two separate views (left and right). Each of those views has a view controller that owns it. Note that the left side includes all of the view, like the search, nav bar, and toolbar. So just create two separate view controllers and add them to a UISplitViewController and you should be golden! The views themselves are created however you normally create views. Storyboards, NIBs, or in code works.

Storyboard - how to make multiple containers embed to the same view controller?

Is there a way for multiple container view's on different scenes to point to one same scene or view controller ? The way I see it, every container has its own embedded scene. What would be the strategy here ? To use the same class on each embedded scene ?
You can hook up multiple container views to the "same" embedded view controller. Control-drag from each container view to the view controller and, when the popup menu appears, choose “embed”.
However, I put "same" in quotes because you need to understand what's going on. At runtime, each container view will create a new instance of the embedded view controller, created by deserializing that part of the storyboard once for each container view. They won't all share the same instance. If you want to keep one persistent view controller that you move around from container to container, you need to do that yourself in code.
Yes, just ctrl-drag from the container to the content view controller and select "embed" You'll also probably want to delete the unreachable view controllers from the storyboard.

accessing 2 viewControllers

I am creating an ipad application, I two view controllers the size screen of an iphone 5 and I would like to show both of them on the ipad screen, as two distinct UIViewControllers though. Is there a way to do it?
I have tried to alloc the second viewcnotrller in the viewdidload of the first, but what I notice is that it alloc the first and the second, but the first is not accessible any more (it looks just like a still image).
You can do it very easily in the storyboard. Just add 2 container views (next to the regular UIViews in the object list) to your controller's view, and size them how you want. You will automatically get 2 view controllers connected to the container views by an embed segue. Just change the class of these 2 controllers to your custom classes, and you should be good to go. If you need to get a reference to these controllers from the main controller, you can get it from the childViewControllers property. Your main controller (assuming it's the initial controller) and the 2 child controllers will all be instantiated at start up with no code necessary.
Check out view controller containment, in which you have a container view controller, which then can load one or more children view controllers. Also see the relevant section in the View Controller Programming Guide. Also see the WWDC 2011 session, Implementing UIViewController Containment.

Resources