iPhone: a same bottom bar for several views

I would like to add a bottom bar in a view and then keep it there while I browse several views (each one with a nab bar). I hope it will follow Apple guidelines (I already know that only the nab bar is recommended) or at least the app is accepted. For doing so, I have added a UIView to the application window. This UIView contains a UITabBarController which contains the navigation controllers (each one as a rootviewController) of each one of the items of the bar:

UIWindow *window=[[UIApplication sharedApplication] keyWindow];
MyUIViewController *mv=[[MyUIViewController alloc]init];
UINavigationController *navd = [[UINavigationController alloc] initWithRootViewController:mv];
navd.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"MyItem" image:[UIImage imageNamed:@"myImage.png"] tag:0];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:navd];
UITabBarController *tbarController=[[UITabBarController alloc] init]; 
tbarController.viewControllers = controllers; 
tbarController.customizableViewControllers = controllers;
UIView *vistaBarraInferior=tbarController.view;
[window addSubview:vistaBarraInferior]; 
[window makeKeyAndVisible];

It works, but my problem appears when I would like to go back and exit the ivies with UITabBarController. If I write:

[self.tabBarController.view removeFromSuperview];
[self.parentViewController.navigationController popViewControllerAnimated:YES];

The tab bar is removed from the current view, but I can´t reach the previous view (the root view I had had before doing anything), because I have overwritten it with the 'initWithRootViewController'.

Is it any other way to make it easier or make this work out?

Thanks in advance.

Upvotes: 0

Views: 163

Answers (3)

If I rewrite this code in other way:

        UITabBarController *tabBar=[[UITabBarController alloc]init];
        tabBar.title=@"MyTitle";
        NSMutableArray *items=[[NSMutableArray alloc]init];
        MyUIViewController *ld = [[MyUIViewController alloc] init];
        [tabBar addChildViewController:ld];            
        UITabBarItem *tabItem=[[UITabBarItem alloc]initWithTitle:@"Item1" image:[UIImage imageNamed:@"myImage.png"] tag:0];
        [items addObject:tabItem];
        [tabItem release];
        [tabBar setToolbarItems:items];
        [self.navigationController pushViewController:tabBar animated:YES];

The result is that the tab bar at the bottom is empty. Have I forgotten to write anything else to show the tab bar item?

If I add a second item, only its title is shown, but neither its image nor anything related to the first item is shown.

Upvotes: 0

Engeor
Engeor

Reputation: 328

If you want to combine tab bar and navigation bar you must use tab bar as a root and for each tab item add one navigation controller. You can't do it in opposite way as tab bar must be always root.

I am not sure what do you want achieve with bottom bar. Whether it should serve as a tab bar or toolbar. Remember if you use tab bar then each tab will have its own stack of views managed by navigation controller. On the other hand toolbar is related to one view.

If you want to create something like overlay toolbar - something that is always on top of views even if they are animating in and out - you need to choose different solution.

For example you can create your own controller that will display your toolbar at bottom and some container view which content will be managed by navigation controller.

Upvotes: 1

mbh
mbh

Reputation: 3312

You should use a tabbarcontroller with array of navigation controllers. See apple sample code here for example.

I went through your code and it does not look right on different levels. For example you are calling initwithviewcontroller but passing a uiview. That is not correct.

Upvotes: 1

Related Questions