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.
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.
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.
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.
Related
I'm looking for a way to have a navigation bar not show up on a screen at all.
I know there is a setNavigationBarHidden:animated: to use on the navigationbar, but this still shows the bar on the screen for a split second before removing and readjusting the screen dimensions.
I've tried moving the setNavigationBarHidden:animated: to the viewDidLoad:, viewWillAppear: etc. and set it hidden on the previous activities viewWillDisappear: but it will shows up on the next screen.
How do I make it such that the view loads with out a navigationbar on it before it is shown?
Thanks,
Dman
I'm hiding the navigation bar for the UINavigationController in the AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method and works fine for me. At some point, while navigating through the screens, I'm showing it then hide it again. Works like a charm.
UIViewController *controller = [[UIViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController: controller];
navCtrl.navigationBarHidden = YES;
self.window.rootViewController = navCtrl;
[controller release];
[navCtrl release];
Hope this helps.
P.S. Actually I never show the navigationBar of the navigation controller again. On the controllers that need the bar I'm adding a new one.
You could try and do
setNavigationBarHidden:animated:
in your view which is calling the view where you don't want it to be displayed just before you call your new view. If they have the same navigationcontroller, it should already be hidden if it appears
if not, you try to set it on the navigation oder viewcontroller before your present it...
I have created a tabbarcontroller (with its default two view controllers) using interface builder in XCode 4.2.
But, when I run the application, the tab bar seems to be locked and I can't choose the other tab. Why is that?
PS: I haven't changed any property of the tab bar or tabbarcontroller in XCode.
How did you go about creating the tab bar? Did you have an initial view and then go to the Edit menu -> Embed In -> Tabbar Controller or did you start with nothing and drag in tab bar controller?
Either way, I just created a project with a single view and tried both ways - but the tab still worked. (if you do it by dragging the tab bar controller from the utility pane, you have to also select 'is initial view controller' if you replace the original view created with the project.
EDIT after your comments:
You don't really need to synthesize the tab bar controller in your AppDelegate - storyboarding will take care of this and you can reference it from code without needing generated synthesizers. Just design the layout in storyboard first by dragging in a tabbar controller (this will automatically create the two view controllers by default). Then select the tabbar controller and under the utilities panel, you'll see the 'is initial view controller' checkbox. Make sure it's checked. Then run your project.
Apologies - assumed because you were using XCode 4.2 that you were using storyboards. Perhaps you could try instantiating a tabbar controller fully in code instead of using IB at all. This is how I usually do it....
// Create the main tabbar controller
tabbarController = [[UITabBarController alloc] init];
// Create the first view controller
MyFirstViewController *v1 = [[MyFirstViewController alloc] initWithNibName:#"MyfirstViewController" bundle:nil];
[v1 setTitle:#"Tab1"];
// Create second view controller
MySecondViewController *v2 = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
[v2 setTitle:#"Tab2"];
// Make an array of controllers for the tabbar
NSArray *tabbarControllerArray = [NSArray arrayWithObjects:v1, v2, nil];
// Set the view controllers used by the tabbar controller
[tabbarController setViewControllers:tabbarControllerArray];
// Release views (retained elsewhere)
[v1 release];
[v2 release];
// Add the controller to the subview
[window addSubview:[tabbarController view]];
// Make key and visible
[self.window makeKeyAndVisible];
Give this way a shot and see how you get on.
I am new to iPhone development.
my application is based on UInavigationBar .
I want to add a navigation bar item in one of my xib, but in the .xib i just simulate the navigation bar so i can't drag and drop the item. thank you
You'll want to add the nav bar buttons programmatically. You see, your xib has a view that is shown within the content view of the UINavigationController. It is the UINavigationBar to which your app has access and which controls the nav bar items. As you point out, your xib has just a placeholder for the nav bar, which is really a convenience for you so your view is sized correctly as you lay it out.
In your UIViewController for the xib, you an add view-appropriate nav bar items with code something like
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"View" style:UIBarButtonItemStylePlain
target:self
action:#selector(viewControls:)] autorelease];
Does that make sense?
In order to be able to add items into a UINavigationBar, you need to first add a UINavigationBar to your view and then add items to it.
You cannot drag and drop items on a simulated navigation bar. A simulated Navigation Bar is just there to make sure you have a correct estimate of the view size available to you if you are adding a Navigation Bar by some other means or from code.
You should be using a UINavigationController for a navigation based hierarchy. That will take care of a lot of the lower details of how to make navigation work as you would like it to. I would also recommend setting that all up programmatically. Here is how you would do that.
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
Then when you want to go to a new view controller (the animated sliding that you normally see), you do this
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
If you want to change the title or the buttons, you do this
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:#"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];
I am trying to nest a UITabBar inside another one, like so:
UITabBarController *tabController = [[UITabBarController alloc] init];
tabController.delegate = self;
UITabBarController *friendsTabController = [[UITabBarController alloc] init];
FindFriendsViewController *findFriendsController = [[FindFriendsViewController alloc] initWithNibName:#"FindFriendsViewController" bundle:nil];
findFriendsController.rootViewController = self;
UINavigationController *findFriendsNavController = [[UINavigationController alloc] initWithRootViewController:findFriendsController];
findFriendsNavController.tabBarItem.title = NSLocalizedString(#"Add", nil);
friendsTabController.viewControllers = [NSArray arrayWithObjects:friendStreamController, friendListController, findFriendsNavController, nil];
tabController.viewControllers = [NSArray arrayWithObjects:nearbyNavController, friendsTabController, meController, checkInController, logController, nil];
(obviously, some of the code, such as the other tabs is ommitted for brevity)
When I do this, and click on the "Friends" tab of the outer tab bar, the inner tab bar appears directly above the outer one. I don't want this. I would like the inner tab bar to take the place (at the bottom of the window) of the outer one when it is selected.
I've tried doing this by hiding the outer tab bar, by setting it to hidden in the UITabBarController delegate method, and I've tried setting the frame of the inner tab bar to manually position it at the bottom of the screen like so:
friendsTabController.tabBar.frame = CGRectMake(0, 430, 320, 50);
When I hide the outer tab bar the inner one still appears the same distance from the bottom edge of the screen, and the outer one is replaced by a blank rectangle.
When I try setting the frame of the inner tab bar, the only part of it that is visible is any part that would be outside the frame of the outer tab bar.
I've tried googling this to no avail. Surely this kind of navigation is not so rare? Before attempting this I had assumed it would be supported in the SDK.
Any ideas?
TIA,
Tom
You could try having a single tab-bar controller in your app. When a user clicks on a tab that leads to a new set of tabs, replace all of the tabs in the single-tab-bar controller. I'm not sure you navigate "back" to the top level, but I presume you have something in mind.
I dont think this is great UI, but you can probably make it work this way.
I think a better solution to your problem would be to add a containerView to the viewController in the tab that you would like to nest the second tabBarController. Then you can add childViewControllers to that containerView and create a custom segue to those childViewControllers in much the same way a tabBarController does. This gives you the option to put your new custom "tab bar" ( I recommend a segmentedControl ) anywhere you like in the viewController containing the containerView. I think it's a more acceptable way to handle a nested tabBarController. Also the user will not be confused with a tabBar suddenly changing appearance.
HI all,
i am having navigation based application, in which i need too implement tab bars ,in one of view.
in one view i need 5 tabs, can any one please suggest me to create tab bars programmatically,, ? each tab should navigate to another xib.
Suggestions are always appreciated.
regards
Here is a sample code from Apple to create tab bar programmatically:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
More here
It's worth noting that a tab bar controller implemented inside a navigation controller is unsupported by Apple. From the same doc linked to by vodhkang above:
Note: Although a navigation controller can be embedded inside a tab, the reverse is not true. Presenting a tab bar interface from within a navigation interface is potentially confusing for users. A navigation interface uses one or more custom view controllers to present an interface focused on one goal, which is usually the management of a specific type of data. By contrast, the tabs of a tab bar interface can reflect completely different purposes in an application and need not be related in any way. In addition, pushing a tab bar controller on a navigation stack would cause the tabs to be displayed for that screen only and not for any others.
So instead of using UITabBarController consider implementing a tab bar with a UIViewController as the tab bar's delegate.