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.
Related
It seems there are several approaches to creating a UINavigationController to be the very first controller. Perhaps the easiest way is to simply click the View Controller in Storyboard and embed it in a navigation controller. But I would like to know the best approach when doing this only in code.
You can subclass UINavigationController, import the first view controller, and in viewDidLoad alloc and init an instance, then add it as a childViewController. In Storyboard replace the default view controller with a navigation controller and set the class to your nav controller. Note that in previous versions of iOS it was not recommended to subclass UINavigationController.
Or you can create another UIViewController, alloc init the first view controller, then alloc init a UINavigationController with that view controller as the root, add the navigation controller as a child view controller of this view controller, and add the navigation controller's view as a subview of this view controller's view. Change the class of the view controller in Storyboard. This is an awkward setup though, because you create a view controller whose purpose is to add a nav controller but it's not a nav controller itself.
I've read about another approach which involves creating the UINavigationController in the AppDelegate. Perhaps there are even more solutions.
What is the most appropriate approach, working in the latest development environment, targeting iOS 8+?
If you're starting with a controller in the storyboard, you only need to add two lines in the app delegate to embed that controller in a navigation controller. If you want to do it in code, I think this is the simplest way,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
self.window.rootViewController = nav;
return YES;
}
If you want to do it entirely in code, with no storyboard, then you need to create the window, the navigation controller, and its root view controller. You also need to click on the project icon in the files list, and in the "General" tab, delete the word "main" from the "Main Interface" pull down (that entry tells the system to start with a storyboard named "main.storyboard").
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *vc = [ViewController new]; // You need to create this controller's view in its loadView method
vc.title = #"Root View Controller";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
The root View View controller is Navigation controller and its first level is a TabViewController.One of the tab item is a TableViewController.
Here is the relationship:
However the navigation bar overlap the table view:
I have also set simulated metrics,So what can be the problem??
Thanks for any help.
Simulated metrics are just that, simulated. They do not actually apply to the compiled product.
To fix this, I find it easiest to set the edgesforextendedlayout with the various values of edge values. Usually all but the top.
The rootViewController should be the UITabBarController. Follow this code:
1.Make the UITabBarController the rootViewController in the application delegate or in your main.storyboard set it as the initial View Controller.
2.In the UITabBarController.m place this code there to create the UINavigationController with a UIViewController embeded inside of it.
//Inside UITabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *vc = [[UIViewController alloc]init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
self.viewControllers = #[navCon];
}
Get rid of the navigation controller in the beginning and, instead, embed the tableviewcontroller inside a navigation controller.
(Select the view controller -- Click "editor" > "embed in" > "navigation controller").
Make sure the tab bar controller is the root view controller as well
This will also fix the overlapping issue
Additional info >>
Just noticed that in my application didFinishLaunchingWithOptions: i've got this
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIViewController *viewController = [[UIViewController alloc] init];
viewController = [storyboard instantiateViewControllerWithIdentifier:#"frontScreen"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Is this part of the problem?
R
=================
Hi I'm developing an app in storyboard.
I'm using the publicly available SWRevealViewController to manage some navigation bar reveal menus.
I've set up a simple UIViewController, lets call it WelcomeViewController. I've dragged a segue from the RevealViewController to my new WelcomeViewController and then I've embed in a Navigation Controller. To the navigation bar I've added a menu reveal button.
All looks good in the storyboard, however when I run in the simulator the navigation bar isn't there.
I've added
self.navigationController.navigationBarHidden = NO;
in the viewWillAppear method - to no avail.
I should mention that I've got an entire branch of viewcontrollers and functionality already coming off the revealviewcontroller with no problems.
Any help. I've search stack overflow but found nothing that matches my specific problems. All advice gratefully received.
R
The storyboard entry point needs to be from the navigation controller and not the main view controller. That should make the navigation toolbar show up in the simulator.
Try adding
[[UIApplication sharedApplication] setStatusBarHidden:NO];
in the view controller's viewDidLoad method
Because the default navigation bar property translucent is set to YES
you can set it to NO on your second view page on viewDidLoad.
navi.translucent = NO;
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.
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.