Setting custom UITabBarItem programmatically? - iphone

In iOS, the TabBar property in the TabBarController is read only. How can I associate a custom item with a particular view controller? How do I access the UITabBarItems inside the tabBar?
Like this
CustomView *custom = [[CustomView alloc] init];
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:#"Custom" image:[UIImage imageNamed:#"custom.png"] tag:0];
SecondView *second = [[SecondView alloc] init];
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:#"Next" image:[UIImage imageNamed:#"next.png"] tag:1];
NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
[tabBarController setViewControllers:views];
//how do I set the individual TabBarItems (customTab,secondTab) to be associated
//with the views in question? tabBarController.tabBar is read only

Inside each view controller, you can set a tabBarItem property. If the view controller is owned by a UITabBarViewController the associated item on the tab bar will be updated accordingly.
Something like this
-(void)viewDidLoad {
[super viewDidLoad];
UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
[self setTabBarItem:tbi]
[tbi release];
}
You are not restricted to perform this operation in the viewDidLoad method, obviously.

Related

change a view controller of UITabBarController

In a UITabBarController, upon selecting a tab, I want that tab's UIViewController to change (assign a new viewcontroller). I'm trying this-
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
But it gives error. How to assign a new UIViewController and refresh instantly?
This is based on femina's answer but doesn't require you to build the whole array of view controllers, it just lets you replace an existing view controller with another. In my case I wanted to swap in a different xib file for the iPhone 5 screen:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
Tracking *tracking568h = [[Tracking alloc] initWithNibName:#"Tracking-568h" bundle:nil];
tracking568h.title = [[viewControllers objectAtIndex:0] title];
tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
[viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
[tracking568h release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
This changes the view controller for the first tab, keeping the same tab icon and label.
Please see this code , it offers 2 tabbar's with navigation.
In the AppDelegate.h please declare
UINavigationController *nav1;
UINavigationController *nav2;
UITabBarController *tab;
And in the Appdelegate.m , in the didFinishLaunchingWithOptions please add:-
tab = [[UITabBarController alloc]init];
ViewController *view1 = [[ViewController alloc]init];
nav1= [[UINavigationController alloc]initWithRootViewController:view1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:#"Add" image:[UIImage imageNamed:#"Plus.png"] tag:1];
view1.title = #"Add";
[view1 setTabBarItem:tab1];
SettingsViewController *view2 = [[SettingsViewController alloc]init];
nav2= [[UINavigationController alloc]initWithRootViewController:view2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:#"Setting" image:[UIImage imageNamed:#"settings.png"] tag:2];
view2.title = #"Setting";
[view2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = tab;
Also check this link for further implementation ... Hope this helps :)
UItabBar changing View Controllers
In the AppDelegate.h
UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;
in the Appdelegate.m
in the didFinishLaunchingWithOptions
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
vc1 = [[UIViewController alloc] init];
vc1.title = #"A";
[listOfViewControllers addObject:vc1];
vc2 = [[UIViewController alloc] init];
vc2.title = #"B";
[listOfViewControllers addObject:vc2];
vc3 = [[UIViewController alloc] init];
vc3.title = #"C";
[listOfViewControllers addObject:vc3];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];

How to set Tabbar integration in iPhone?

I am working on an iPhone app, using UITabBar to develop it. Only the second tabbar title visible in tabbar, but first tabbar title not show in tabbar, how to fix this? please help me,
Thanks in Advance.
below is the source code for your reference.
NSMutableArray *items = [[NSMutableArray alloc]init];
tab =[[UITabBar alloc]initWithFrame:CGRectMake(0, 300, 320, 44)];
[self.tabBarItem initWithTitle:#"sample1" image:nil tag:111];
[items addObject:self.tabBarItem];
[self.tabBarItem initWithTitle:#"sample2" image:nil tag:101];
[items addObject:self.tabBarItem];
[tab setItems:items animated:YES];
[self.view addSubview:tab];
Dont navigate the view controllers, because then your controller which contains the TabBar will get popped out.
When we use UITabBar we dont use navigation in this way.
Just addSubView the new controllers with such frame so that your TabBar will alwaz be visible.
You have to add that tabbarItem in UITabBar.
NSArray *items = [[NSArray alloc]init];
[items addObject:self.tabBarItem]; //add all your tabBarItems in an array
[tab setItems:items animated:YES]; //then set all your tabBarItems in UITabBar like this
Actually you are using same instance of tabBarItem thats'y you are getting only second button.
Do this way, and Please if this helps you then accept the answer.
UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 725, 768, 49)];
NSMutableArray *items = [[NSMutableArray alloc]init];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"first" image:nil tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:#"second" image:nil tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:#"third" image:nil tag:3];
UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:#"fourth" image:nil tag:4];
[items addObject:item1];
[items addObject:item2];
[items addObject:item3];
[items addObject:item4];
[tabBar setItems:items];
[self.view addSubview:tabBar];
That's not the way you do to create and add a TabBar programmatically.
You'd rather use this other method: https://stackoverflow.com/a/3844365/655221
Good luck.

Custom UITabBar that expand on selection

I am trying to achieve the below attach image on the UITabbar.
http://i.imgur.com/7Tukx.png
It's pretty straight forward, on selection the tab expand with a label to display text and on selection on another tab, the selected tab will collapse.
I am not really sure how to do this with the UItabbar.
I will appreciate if anyone could point me in the right direction.
for the custom tab you use this method on appdelegate class
this method create two tab bar item.
and you set your image..
-(void)SetTabs
{
tabBarController = [[UITabBarController alloc]init];
NSMutableArray *localControllerArray = [[NSMutableArray alloc]initWithCapacity:2];
UIImage *image = [UIImage imageNamed:#"home.png"];
GreetingCardsViewController *GreetingCardsView = [[[GreetingCardsViewController alloc] initWithNibName:#"GreetingCardsViewController" bundle:nil] autorelease];
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:#"Home" image:image tag:0];
UINavigationController *GreetingCardsViewNavigationController = [[UINavigationController alloc]initWithRootViewController:GreetingCardsView];
GreetingCardsView.tabBarItem = item;
[localControllerArray addObject:GreetingCardsViewNavigationController];
UIImage *image1 = [UIImage imageNamed:#"heart1.png"];
Cards *CardView = [[[Cards alloc] initWithNibName:#"Cards" bundle:nil] autorelease];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"Cards" image:image1 tag:1];
UINavigationController *CardNavigationController = [[UINavigationController alloc]initWithRootViewController:CardView];
CardNavigationController.tabBarItem = item1;
[localControllerArray addObject:CardNavigationController];
[tabBarController setViewControllers:localControllerArray];
[_window addSubview:tabBarController.view];
[localControllerArray release];
}
I'm not entirely sure what you're after, however I think this may be relevant to you. You could use the basic concept and tweak it to what you need.

programmatically create TABBAR

i created TAb bar programatically in this manner
UITabBarController *tabBarController = [[UITabBarController alloc] init];
contacts *vc1 = [[contacts alloc]init];
vc1.tabBarItem.image=[UIImage imageNamed:#"contacts.png"];
search* vc2 = [[search alloc] init];
vc2.tabBarItem.image=[UIImage imageNamed:#"search.png"];
a1* vc3 = [[a1 alloc] init];
a2 *vc4 = [[a2 alloc] init];
a3 *vc5 = [[a3 alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2,vc3,vc4,vc5, nil];
tabBarController.viewControllers = controllers;
[self.view addSubview:detailNavCont];
// Add the tab bar controller's current view as a subview of the window
[self.view addSubview:tabBarController.view];
What i want to accomplish is, i want to assign images to tab bar, firstly i tried on first 2 tabs, is showing a blue block instead of image.
secondly, when we create tab bar through Interface builder, there are custom tab bar item, like, contacts, search, bookmark ,compose etc.
so, if i want to assign contacts or search image to my tab bar items,which looks like the one in IB, how can i do it??
regards
Try something like this
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
HomeTabViewController *ptr_homeTab;
ptr_homeTab = [[HomeTabViewController alloc]initWithNibName:#"HomeTabViewController" bundle:nil];
UINavigationController *homeNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_homeTab];
homeNavBar.tabBarItem.title=#"Home";
homeNavBar.tabBarItem.image=[UIImage imageNamed:#"home.png"];
[ptr_homeTab release];
myHospitalviewController=[[MyHospitalViewController alloc]initWithNibName:#"MyHospitalViewController" bundle:nil];
UINavigationController *myHospitalNavBar=[[UINavigationController alloc]initWithRootViewController:myHospitalviewController];
myHospitalNavBar.title=#"My Hospital";
myHospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"myhospital.png"];
[myHospitalviewController release];
viewController = [[TreatMentiViewController alloc]initWithNibName:#"TreatMentiViewController" bundle:nil];
UINavigationController *hospitalNavBar=[[UINavigationController alloc]initWithRootViewController:viewController];
hospitalNavBar.tabBarItem.title=#"Hospital";
hospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"hospital.png"];
[viewController release];
PersonalMedicineViewController *ptr_PersonalMedicine = [[PersonalMedicineViewController alloc] initWithNibName:#"PersonalMedicineViewController" bundle:nil];
UINavigationController *managerNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_PersonalMedicine];
managerNavBar.tabBarItem.title=#"Manager";
managerNavBar.tabBarItem.image=[UIImage imageNamed:#"manager.png"];
[ptr_PersonalMedicine release];
[localViewControllersArray addObject:homeNavBar];
[localViewControllersArray addObject:hospitalNavBar];
[localViewControllersArray addObject:myHospitalNavBar];
[localViewControllersArray addObject:managerNavBar];
[homeNavBar release];
[hospitalNavBar release];
[myHospitalNavBar release];
[managerNavBar release];
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[localViewControllersArray release];
[window addSubview:tabBars.view];
Your images need to have a proper mask. You should save as png.

iPhone:Tabbar hides when pushing from TableView to UIViewController

I have four Tab bar items in a Tab bar which is being bottom of the view where i have the TableView. I am adding Tab bar and items programmatically (Refer below code) not through I.B.
Click on first three Tab bar items, will show the data in the same TableView itself. But clicking on last Tab bar items will push to another UIViewcontroller and show the data there. The problem here is, when i push to the viewController when clicking on last Tab bar item, main "Tab bar" is getting removed.
Tab bar code:
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 376, 320, 44)];
item1 = [[UITabBarItem alloc] initWithTitle:#"First Tab" image:[UIImage imageNamed:#"first.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"Second Tab" image:[UIImage imageNamed:#"second.png"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Third Tab" image:[UIImage imageNamed:#"third.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Fourth Tab" image:[UIImage imageNamed:#"fourth.png"] tag:3];
item5 = [[UITabBarItem alloc] initWithTitle:#"Fifth Tab" image:[UIImage imageNamed:#"fifth.png"] tag:4];
NSArray *items = [NSArray arrayWithObjects: item1,item2,item3,item4, item5, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:item1];
tabBar.delegate=self;
[self.view addSubview:tabBar];
Push controller code clicking from last Tab bar item:
myViewController = [ [MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
myViewController.hidesBottomBarWhenPushed=NO;
[[self navigationController] pushViewController:myViewController animated:NO];
I am not seeing bottom Tab bar when i push my current TableView to myViewController. I am seeing full screen view there. I want to see bottom Tab bar always when every tab item clicked.
What might be the problem here? Could someone who come across this issue, please share your suggestion to me?
Thank you.
You are using the TabBar itself (as view) as the main view initially.
Use UITabBarController like this:
//tabBarController is defined in the interface (the .h file)
tabBarController = [[UITabBarController alloc]init];
firstViewController = [[UIViewController alloc] init];
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:1] autorelease];
[firstViewController setTabBarItem:item2];
secondViewController = [[SecondViewController alloc]init];
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:#"Sec" image:nil tag:1] autorelease];
[secondViewController setTabBarItem:item2];
//init the tab bar controller populated with two view controllers
[tabBarController setViewControllers:[NSArray arrayWithObjects:firstViewController,secondViewController,nil] animated:NO];
[window addSubview:tabBarController.view];