4

I am working on an app in which I have to display multiple view controllers side by side (split views). For this purpose I have added views as child view controller.

OBJECTIVE: I want to show navigation bar on one child view controller along with already shown separate navigation bar on parent view controller.

PROBLEM: Navigation bar doesn't get shown on child view controller.

EDIT: I have also set navigation bar of parent view controller as hidden but when child view controller get's called, the navigation bar gets appeared on parent view controller, not on child view controller.

Code to add child view controller is:

    MyChildViewController *childViewController = [[MyChildViewController alloc] initWithNibName:@"MyChildViewController" bundle:nil];

    [self addChildViewController:childViewController];
    [childViewController.view setFrame:CGRectMake(0.0f, 0.0f, self.rightContainerView.frame.size.width, self.rightContainerView.frame.size.height)];
    [self.rightContainerView addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];

This code is working fine and child view controller gets add perfectly. I want to know is it possible or not?

Thanks in advance.

3
  • can you add navigation bar manually for this childviewcontroller
    – Thukaram
    Apr 8, 2014 at 11:43
  • I am adding navigation bar through code in child view controller. anything added to navigation bar does not get shown/added. Apr 8, 2014 at 11:45
  • I can make a view in child view controller resembling like a navigation bar but that would be my final alternative. Right now I am looking for a concrete solution Apr 8, 2014 at 11:46

4 Answers 4

11

I solved this problem by myself through following way:

MyChildViewController *childViewController = [[MyChildViewController alloc] initWithNibName:@"MyChildViewController" bundle:nil];
[childViewController.view setFrame:CGRectMake(0.0f, 0.0f, self.rightContainerView.frame.size.width, self.rightContainerView.frame.size.height)];

UINavigationController *childNavController = [[UINavigationController alloc] initWithRootViewController:childViewController];
childNavController.view.frame = childViewController.view.frame;

[self addChildViewController:childNavController];
[self.rightContainerView addSubview:childNavController.view];
[childNavController didMoveToParentViewController:self];

Now when I add navigation bar in MyChildViewController, it gets added in child view controller and does not affect navigation bar of parent view controller. The navigationController property of child view controller is also different than navigationController property of parent view controller and both have their own navigation stacks.

0
3

add Navigation bar like this

enter image description here

It will be appear in your all view controller

Another way to do:

Put this code into didFinishLaunchingWithOptions method in appdelegate.m file.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ECViewController * ec = [[ECViewController alloc] initWithNibName:@"ECViewController" bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:ec];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;

Add a Nib file(ECViewController) by simply following these steps:

1: Right click in project root

2: Add new file

3: Go to user interface.

4: Select view

5: Give it name same as your view controller name i gave ECViewController in my case.

Click on newly created nib file

Click on file owner fist yellow box on left side. make an connection with view by simple dragging with control keyword.click on view when popup appear.

Now goto identity inspector (fourth section from staring in left side).

Write your class name in Class name textBox appeared.

By this you can able to open an xib in iOS7 if you don't want to use storyboard.

Now if you need view controller with navigation controller.

Then open your view controller by this way.

 ECViewController1 *v = [[ECViewController1 alloc]initWithNibName:@"ECViewController1" bundle:nil];

    [self.navigationController pushViewController:v animated:YES];

Its too late for this edited answer, Hope it will help someone other.

3
  • I am not using storyboards. And this option is disabled while using nib files. Apr 8, 2014 at 11:36
  • Well may be you didn't get my question.. I asked for showing navigation bar in child view controller where parent view controller is also showing navigation bar.. that is two navigation bars at a time on the screen with different navigation controllers and stack.. I am using split views. anyways thanks a lot for your efforts. i managed the work by doing some logics.. i have answered my own question. you can see that.. :-) May 28, 2014 at 9:13
  • its ok i thought you need navigation bar in all view controller :P, So i answered according it.thanks for reading my answer May 28, 2014 at 9:18
1

Set the y position of child view correctly.

MyChildViewController *childViewController = [[MyChildViewController alloc] initWithNibName:@"MyChildViewController" bundle:nil];

    [self addChildViewController:childViewController];
    [childViewController.view setFrame:CGRectMake(0.0f, 44.0f, self.rightContainerView.frame.size.width, self.rightContainerView.frame.size.height)];
    [self.rightContainerView addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];
0
-1

I have found the link from stackoverflow we need to add navigation bar manually there is no alternate for this.

1
  • In the above mentioned link's post, the OP has asked about accessing navigation bar of parent view controller. I do not want that. I want to create new separate navigation bar to show in just child view controller. Apr 8, 2014 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.