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.
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.
add Navigation bar like this
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.
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];
I have found the link from stackoverflow we need to add navigation bar manually there is no alternate for this.
Related
Adding tab bar controller to the UIView by [self.view addSubview:tabBarController.view];
is not loaded the content view controllers view, while selecting the tab bar items it crashed. When set the tab bar controller to the root view controller its working fine. How to set tab bar controller into UIView so that I can set another view for some other purpose like iAd?
You just create one tabbar controller in app delegate but don't add it to window, and in view controller you have just add these:
gObjAppDelegate.tabBarController = [[UITabBarController alloc] init];
gObjAppDelegate.tabBarController.viewControllers = #[viewController1, viewController2];
[gObjAppDelegate.tabBarController setSelectedIndex:0];
[self.navigationController pushViewController:gObjAppDelegate.tabBarController animated:YES];
gObjAppDelegate is shared singleton instance of app delegate.This way it worked for me.
I just changed my app and Im quite confused. It started with the root view that then pushed a second view, there is a button on that view that pushes another view...
So I decided to instead present that second view controller but now the other view cant be pushed from the second.
Code From Root View:
//This works
[[self navigationController] presentViewController:secondViewController
animated:YES completion:nil];
Code From Second View:
//This Does not work
[[self navigationController] pushViewController:locactionView animated:YES];
Edit: Sorry for the lack of detail. No exception is thrown it simply doesn't push the "Location View." Before I presented the view I pushed it and everything worked fine. Also when the view was originally pushed the navigation bar was visible, now that the View Controller is being presented I can't push a view or see the Nav Bar. I hope this helps. I don't know what code I could add because I only changed a single line before the problem occurred.
In your AppDelegate you could initialize your NavigationController with the rootViewController first, and then you could push the viewControllers on the stack, that could solve your problem
YourNavigationController *yourNavigationController = [[YourNavigationController alloc] initWithRootViewController:self.firstViewController];
If you have your hierarchy set up like this:
self.rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController"
bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
You should not have any issues with this stuff. Your issue might be that you are trying to push onto the navigation controller from a view controller you presented modally which you cant do
I have been seeing this issue and resolving it by manually creating UINavigationController in the code. Could someone please tell me when I add Navigation Bar from XIB's Attribute Inspector -> Set Top bar to Black Navigation bar, it gets displayed in the XIB but when I run the program, it doesn't appear! I noticed that self.NavigationController was coming nil so I added UINavigationController in my XIB and assign NIB but still it's nil! What's wrong with this? Do I need any additional settings?
[EDIT1]
I tried adding it like below and it works but I want parent class to forward rotation and appearance events to child controller automatically. If I do following it won't send them because I am adding nvc as child and not marketsListViewController. So I thought I have to subclass UINavigationController. See EDIT2.
self.marketsListViewController = [[MarketsListViewController alloc] initWithNibName:#"MarketsListViewController" bundle:nil];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:self.marketsListViewController];
nvc.navigationBar.barStyle = UIBarStyleBlack;
[self.marketsListView addSubview:nvc.view];
[self addChildViewController:nvc];
[nvc didMoveToParentViewController:self];
[EDIT2]
I have subclassed marketsListViewController to UINavigationController and thought following will work but it doesn't. It just displays navigation bar, UITableView doesn't get displayed!
self.marketsListViewController = [[MarketsListViewController alloc] initWithNibName:#"MarketsListViewController" bundle:nil];
self.marketsListViewController.navigationBar.barStyle = UIBarStyleBlack;
[self.marketsListView addSubview:self.marketsListViewController.view];
[self addChildViewController:self.marketsListViewController];
[self.marketsListViewController didMoveToParentViewController:self];
[EDIT3]
I was wrong in Edit1 that children controller won't get rotation events when I add child as navigation controller's root controller. Parent still sends all the events automatically and that's what I want! :)
Could someone please tell me when I add Navigation Bar from XIB's
Attribute Inspector -> Set Top bar to Black Navigation bar, it gets
displayed in the XIB but when I run the program, it doesn't appear!
What you see in you .xib file is just there to help you get your layout right and get a good idea of what it'll look like. When you have an app that uses a navigation bar, the bar is almost always managed by a navigation view controller (UINavigationController). The view controllers that the navigation controller hosts don't worry about the nav bar -- their view hierarchies go in the space beneath the bar.
So, the right way to get a navigation bar in your app is almost certainly to use a navigation controller. If you only have one view controller, make that that the navigation controller's root view controller (note: the root view controller of the window will be the navigation view controller). You can change the appearance of the bar from your view controller by setting the navigation bar's style:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Update: In the code you posted, you're just creating a new navigation controller, but not doing anything with it. I alluded above the the fact that (in most cases) the navigation controller should be the window's root view controller. It's fine to create your navigation controller in code, but you'll usually do it in the app delegate, and once you create it you'll set it as the window's root view controller. Here's an example borrowed from a project made fresh from Xcode's master/detail template (sans storyboards):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MMMasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Note that after creating the window and the navigation controller, the code sets the nav controller as the window's root view controller.
I'm working on adopting slide (split) view controller to my project.
JT, DD, ZUUI, JW, ECS.....
All these sources suggesting initialize my root view controller in appDelegate.
Something like this....
MyMainViewController *controller = [MyMainController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:Controller];
.........
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Problem is I can't make my view visible, my app show blank page only with a empty navigation bar.
I customized my main view using AQGrid, is this causing a problem?
My view looks different to storyboard look. (because I customized it.)
So when I do initialize I'm using "self.storyboard initialize......method".
But in appdelegate, I can't use that method.
Simply, I can't make this view hierarchy because when I initialize my view it is not visible.
ZUUIRevealController is parent of:
UINavigationController is parent of:
FrontViewController
If you are using a storyboard, don't do any of that. Instead, choose your starting view controller from the storyboard and check the "Is Initial View Controller" in the Attributes inspector.
I have created a tab bar controller as follows:
tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
NSMutableArray *tabsArray = [[NSMutableArray alloc] init];
//tab1: dive info
LogsDetailDive *logsDetailDive = [[LogsDetailDive alloc] initWithNibName:#"LogsDetailDive" bundle:nil];
[logsDetailDive initWithLogSelected:logSelected:siteSelected];
logsDetailDive.title = #"Info";
logsDetailDive.tabBarItem.image = [UIImage imageNamed:#"/images/logs.png"];
[tabsArray addObject:logsDetailDive];
//tab2: deco info
...
//tab3: equipment info
...
//tab3: computer info
...
tabBarController.viewControllers = tabsArray;
[logsDetailDive release];
[self.view addSubview:tabBarController.view];
This tab controller is pushed from a previous table view into the navigation controller.
What I'm trying to get now is to show another view pushed from a tableview in LogsDetailDive, but I really cannot understand what I am missing, since it doesn't work.
Can you suggest something?
Thanks
UITabBarController reference
Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
So the bottom line, this should not be done, and will cause some problems. If you need a tabbar layout you should instead use a toolbar.