3

I have ViewController mainVC - it consist of two parts - navigation view and wrapper view. When user press one of the buttons in NavigationView - one of my ViewControllers - for example vc is loaded inside WrapperView.

    addChildViewController(vc)

   //size adjustment
   vc.view.frame = contentView.bounds
   WrapperView.addSubview(vc.view)

I want to know is it possible to put NavigationController inside of WrapperView - so that my navigation will be visible and user still can change view using buttons in navigation view. How it can be done?

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
moonvader
  • 19,761
  • 18
  • 67
  • 116

2 Answers2

7

Yes. It's totally possible. UINavigationController is just a subclass of UIViewController.

And it's view can be added in any view hierarchy as normal views. So what you can do is add navigation controller to your wrapper view and then add subsequent ViewControllers into the UINavigationController.

So in your case you can do something like this:

    let navController = UINavigationController(rootViewController:mainVC)
    self.addChildViewController(navController)
    navController.view.frame = contentView.bounds
    contentView.addSubview(navController.view)

More view controller could be added directly to your navigationController then

    navController.showViewController(viewController2, sender: nil

Note: Here self is viewcontroller containing your wrapperview.

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
  • can you tell - what is the best way to open another ViewController from ViewController inside WrapperView? – moonvader May 02 '16 at 07:32
  • lets suppose you added. Navigation Controller as viewControlller in Wrapper view, and initiated rootViewController of it as mentioned in my answer. Now you have mainVC from which you need to open another ViewController say VC2? You can simple call, self.navigationController.showViewController(VC2, sender:nil) from inside the mainViewController. – Prajeet Shrestha May 02 '16 at 08:01
  • thank you - the only problem that this method is not working on iOS 7 – moonvader May 02 '16 at 19:07
  • Yes for iOS7 there is another method. pushViewController, use it instead of showViewController. – Prajeet Shrestha May 03 '16 at 01:54
1

Yes it is possible by using UIContainerView. check Apple's documentation for more detail.

You can take container view as your desired size (In your case WrapperView Size) and you will get that size of view connected by special custom segue with your view controller.

Just drag and drop containerview to your viewController from storyboard and you will get better idea.

Update :

Add containerView in your viewController.

Select that container view which is shown beside of your VC connected with storyboard embed segue.

from editor embed in navigation controller

now drag new view controller to canvas and from container view controller ctrl + drag to that new VC and your new VC get resize according to your container view's size and when you push it it remains in that size

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • I added UIContainerView instead of my WrapperView - when i press buttons in NavigationView - ViewControllers in UIContainerView are changing. But when i click on some buttons on one of the ViewControllers that is embedded into navigation controller - new ViewController covers all screen and i don't see NavigationView. – moonvader Apr 30 '16 at 11:39
  • No it shouldn't be like this. I am updating my answer follow accordingly. – Ketan Parmar Apr 30 '16 at 12:02