7

I am current developing an app in iPad. The app consist of only two views, but each view contains a lot of buttons and labels. Current, the layout of the two views is set. Each one is set in a view controller. I also have another view controller which contains the main menu on top and a big container(UIView) in which I hope it will be able to hold the two views I mentioned.

My question is, is there a way to show a view controller inside a view? I want to display one view controller inside that container(UIView) when I click on a button in the main menu, and display another when I click on another button. If my plan is not possible then please make some suggestions to make the same thing work.

Many Thanks!

JLT
  • 3,052
  • 9
  • 39
  • 86

2 Answers2

12

Yes you can easily do it by adding the UIViewController view like below..

_viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self.view addSubview:viewController.view];

As soon as you add viewController.view your viewDidLoad method inside ViewController gets called.

Update: As per UIViewController Class Reference you need to add two more steps when adding a UIViewController as subview inside another ViewController.

[self addChildViewController:viewcontroller];
[self.view addSubview:viewController.view];
[viewcontroller didMoveToParentViewController:self];

Above completes the answer.

Hope this helps. Cheers.

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • ok, a follow up question. what if I want to change the view controller inside the subview. do I need to remove the previous one and add another new one? – JLT Jun 19 '15 at 07:46
  • @EpicNinja I didn't get your question, are you asking if you want to do any changes on the already added viewcontroller? Is so, you don't need to add it again, you can have functions created for specific task and call it with the viewcontroller object you would definitely have. – iphonic Jun 19 '15 at 08:40
  • Uhm, I mean everytime I want to switch to a different view under the container, I ll just add that as a subview? No need to remove the previous view I added,? – JLT Jun 19 '15 at 08:49
  • Yes, no need to remove, you can hide though. – iphonic Jun 19 '15 at 08:51
  • And Yes, don't forget to add `[self addChildViewController:viewcontroller];` and `[viewcontroller didMoveToParentViewController:self];` before and after adding as subView, I missed this point. – iphonic Jun 19 '15 at 09:13
  • Hi @iphonic I encountered a new problem. All of my view controllers are set to iPad Full Screen Landscape. I wish to allow only landscape orientation for the app. So I set the app orientation to only landscape left and right. My problem is that once I add a subview to the container view. The view can be seen added successfully. BUT the root view of the subview seems to be in portrait when the subview was added to the container view. – JLT Jun 24 '15 at 07:02
  • I set the background of the root view of the sub view to green. When the subview was added to the container view. I can see the root view of the subview is in portrait. because of the revealing green background at the bottom. – JLT Jun 24 '15 at 07:04
  • @EpicNinja do you have screenshot to make me understand clearly? – iphonic Jun 24 '15 at 07:10
  • I am afraid the uploading is too slow, and I dont even know if its working. Would you mind if I ll just send you the screen shots? – JLT Jun 24 '15 at 07:28
  • @EpicNinja Yes only screenshot should do. – iphonic Jun 24 '15 at 07:37
  • Can I have your email then? I can't upload the image in here. It took awhile and nothing was upload. – JLT Jun 24 '15 at 07:38
  • I sent you an email just now. Thank you so much for willing to help me solve this problem! :) – JLT Jun 24 '15 at 08:50
  • Seen your screenshot. Can you tell whether are you using **AutoLayout** ? And which iOS version your iPad has? – iphonic Jun 24 '15 at 09:28
  • Yes I am using auto layout. iOS version is 7.1 – JLT Jun 24 '15 at 10:08
  • Okay, since you are using autolayout, so simply adding as subview won't work... see my answer here http://stackoverflow.com/a/30999875/790842. It will help.. – iphonic Jun 24 '15 at 10:39
8

Custom Container View Controllers are just what you need. If you use Interface Builder and storyboards - find a container view, drag it to your view and set the contained class to your view controller.
container view in objects library

If you don't use storyboards, or IB (which i encourage you to do) - follow the link above and implement adding a child view controller to your view controller. Never ever add a subview without previous addChildViewController: call, this may lead to unexpected results. Normally, adding a child view controller should be in this order:

  1. call [self addChildViewController:childvc]
  2. add child's VC view as a subview [self.view addSubview:childvc.view]
  3. call [childvc didMoveToParentViewController:self]

In that case everything will work correctly.

Sega-Zero
  • 3,034
  • 2
  • 22
  • 46
  • Thank you @Sega-Zero. I ll try this. :) – JLT Jun 19 '15 at 08:58
  • Is there any way so i can prevent load view every time ? I have 4 controllers as a child view controller in container view but every time when i changing tab it is loading controller from start. Initially i want to load my view once only. – Mitesh Dobareeya Oct 18 '18 at 05:29
  • maybe UITabBarController with a hidden tabbar would be better in that case? – Sega-Zero Oct 18 '18 at 18:52