Custom Tabbar Controller for iPhone - iphone

I try to create framework for iPhone and try to implement Custom Tabbar Controller in my framework. Any one can help me to create custom tab bar controller for iPhone. I mean programmatically add tab bar controller.

To create tab bar give this code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1, *viewController2,*viewController3;
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewControllerXibname" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewControllerXibname" bundle:nil];
viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewControllerXibname" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
self.tabBarController.moreNavigationController.navigationBar.tintColor =[UIColor darkTextColor];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor yellowColor]]; //change selected image color on tabbatItem
self.tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
[self.tabBarController.tabBar setOpaque:YES];
return YES;
}

This links may be helpful to you:
http://howtomakeiphoneapps.com/how-can-i-add-tabs-programmatically-to-uitabbar/201/
http://kurrytran.blogspot.in/2011/10/ios-5-tutorial-creating-custom-tab-bar.html
http://mobiledevelopertips.com/open-source/ios-open-source-custom-tabbar-controller-bctabbarcontroller.html
Or
The other option is - you can go with the custom buttons which acts like an UITabbarController.
Enjoy Coding :)
Mrunal

Related

Strange behavior with UITableViewController and tabbed navigation

I have an application in which the requirement is to have sections laid out in tabbed format. Under one of the tabs, there will be a table view that has a segue to a detail view. When I setup this transition outside of the tabbed navigation, it works. however, when I try to do this within the tabbed navigation, I get a runtime error and crash. Is there a known issue of trying to have a tableview with segues loaded by a tab?
Thanks! Viv
Following is simple example for How can you use UITabBarController
Firsts Create all object of UIViewController and UINavigationController in AppDelegate.h file and use following method of AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];
self.viewCon=[[ViewController alloc] init];
self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor=[UIColor blackColor];
self.viewCon.title=#"First View";
self.fView=[[FirstViewController alloc] init];
self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
self.FnavCon.navigationBar.tintColor=[UIColor blackColor];
self.fView.title=#"Secound View";
self.sView=[[SecoundViewController alloc] init];
self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
self.sView.title=#"Third View";
.
.
// create UIViewController and UINavigationController As you need
.
.
.
UIImage *img1=[UIImage imageNamed:#"Australia.gif"];
self.tbItem1=[[UITabBarItem alloc] initWithTitle:#"First Page" image:img1 tag:1];
self.viewCon.tabBarItem=self.tbItem1;
UIImage *img2=[UIImage imageNamed:#"Cameroon.gif"];
self.tbItem2=[[UITabBarItem alloc] initWithTitle:#"Secound Page" image:img2 tag:2];
self.fView.tabBarItem=self.tbItem2;
UIImage *img3=[UIImage imageNamed:#"Canada.png"];
self.tbItem3=[[UITabBarItem alloc] initWithTitle:#"Third Page" image:img3 tag:3];
self.sView.tabBarItem=self.tbItem3;
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
[viewArr addObject:self.FnavCon];
[viewArr addObject:self.SnavCon];
self.tbCon=[[UITabBarController alloc] init];
self.tbCon.viewControllers=viewArr;
[self.window addSubview:tbCon.view];
[self.window makeKeyAndVisible];
return YES;
}

Change tab bar item title and color programmaticaly

I have implemented a tab bar controller in my App Delegate, but it's just empty squares in the tab bar. I wish to could change title and images of them and also I want to know how use not only custom image I add, but "default" images implemented in Xcode ("calculator" image, "search" image).
If you have tab bar in a xib, you can see it in tab bar item -> attributes inspector -> Identifier, then there is a list, if you don't want to use custom images. So there is my appDelegate.m code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after app launch
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
UINavigationController *listNavigationController = [[UINavigationController alloc] initWithRootViewController:banksList];
UIViewController *first = [[BIDViewController alloc] initWithNibName:#"BIDViewController" bundle:nil];
UIViewController *second = [[BIDDailyCount alloc] initWithNibName:#"BIDDailyCount" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:first,second,listNavigationController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You have to create your UITabBarItems your self.
In you appdelegate you could do something like:
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
banksList.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
return self;
It might be a good idea to move this to a custom initializer in the controllers for each tab.

Combine UITabBarController with UINavigationController

I tried to use an "tabbed application" with a navigation bar in it. With the default the tab bar works fine, but I just can't gat a navigation bar. I found some stuff about pushing the navigation-bar and stuff like that, but all the stuff I found was some years ago, so don't gonna help me. And the recent stuff is outdated to, since iOS5 and the new version of Xcode..
Could anyone point me in the right direction to combine a to solve this problem?
Keep the following facts in mind please:
I'm developing for iOS5
I'm using Xcode 4.2
Here's how you can achieve it programmatically.
Delete the reference to your main xib in [appName]-Info.plist
In main.m, load your delegate:
int retVal = UIApplicationMain(argc, argv, nil, #"myAppDelegate");
In the app delegate, load the tabBar, the navigation controller and the view in the navigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// View Controllers for tabController (one viewController per tab)
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// first tab has view controller in navigation controller
FirstView *firstView = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView];
[viewControllers addObject:navController];
SecondView *secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[viewControllers addObject:secondView];
// create the tab controller and add the view controllers
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSMutableArray *arrayViewController = [[NSMutableArray alloc] init];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[arrayViewController addObject:navigationController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[arrayViewController addObject:navigationController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = arrayViewController;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In iOS 5 it is no longer tolerated to change the view controller for a tab (no problem before iOS5). The only accepted controller is that defined in IB for that tab. So it is neccessary to install a navigation controller on this tab and give his view the navigation bar. Then you can push or pop your desired views without changing the tab's controller.
The basic theory is that you create a UITabBarController, and then put a UINavigationController inside that, and then put a UIViewController as the root view controller of the navigation controller. bryanmac just answered with a good code sample.

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?
Here the viewcontroller Class is is UITableviewcontroller .
right now if i am adding the tab bar it appears some where in table view. i want to display it on bottom of window and and tableview should scroll separately from Tabbarcontroller
Please help and suggest
Thanks
FirstView *view1 = [[FirstView alloc]init];
SecondView *view2 = [[SecondView alloc] init];
UINavigationController *firstview = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *secondview = [[UINavigationController alloc] initWithRootViewController:view2];
UITabBarController* tabBar = [[UITabBarController alloc] init];
[tabBar setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];
[tabBar setDelegate:self];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBar];
[self.view addSubview:navigationController.view];
Try this. here FirstView, SecondView are the class. if you click the tab, corresponding class (view) will open. And you have declare the tabbar and navigation controller delegate.
first you just select tabbar base application from project template.
now double click on mainwindow.xib its open it in interface builder.
now click on tabbarcontroller and open inspector window.
in above image your are able to see list of viewcontrollers. click on class tab will gives you the option of controller type. see in below image
if your are selecting table view controller then your first controller is table view. and tabbar is not in your view. table is scroll separately.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//************ WRITE THE CODE OVER HERE FOR THE CREATION OF THE TAB BAR BY CODING**************
tabbar_object=[[UITabBarController alloc] init];
self.tabbarViewController=[[[TabBarViewController alloc] initWithNibName:#"TabBarViewController" bundle:nil] autorelease];
self.viewcontroller1=[[[FirstViewControllerForTabBar alloc] initWithNibName:#"FirstViewControllerForTabBar" bundle:nil] autorelease];
self.viewcontroller2=[[[SecondViewControllerForTabBar alloc] initWithNibName:#"SecondViewControllerForTabBar" bundle:nil] autorelease];
UINavigationController *tabbatnavigation=[[UINavigationController alloc] initWithRootViewController:tabbarViewController];
tabbatnavigation.title=#"First Tab";
UINavigationController *navigation1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
navigation1.title=#"Sign Up";
UINavigationController *navigation2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
navigation2.title=#"Sign Out";
tabbar_object.viewControllers=[[NSArray alloc] initWithObjects:navigation1,navigation2,tabbatnavigation, nil];
[tabbar_object setSelectedIndex:0];
//************************************************************************************************
self.window.rootViewController = tabbar_object;
[self.window makeKeyAndVisible];
return YES;
}

How to Use TabBarViewController with a NavigationController

I know I can do this
[self.navigationController pushViewController:self.someUITabBarController animated:YES];
And that means putting UITabBarController on a navigationgController somehow
What about if I want someUITabBarController to be the first controller (the one located on the lowest level) of navigationController?
I simply cannot change the rootViewController of the NavigationController into someUITabBarController
Erm not sure this is what you want. Below this code will be put under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in you "appDelegate" class.
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = ...
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
NSArray *controllers = [NSArray arrayWithObjects:navigationController, nil]; // can add more if you want
[tabController setViewControllers:controllers];
// this is for custom title and image in the tabBar item
navigationController.tabBarItem.title = #"abc";
[navigationController.tabBarItem setImage:[UIImage imageNamed:#"abc.png"]];
self.window.rootViewController = tabController; // or [self.window addSubview: tabController.view];
[self.window makeKeyAndVisible];
I am not sure if this works. But try this,
UINavigationController *navCont = [[UINavigationController alloc] init];
[navCont pushViewController:navCont animated:NO];