How to set Tabbar integration in iPhone? - 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.

Related

Set UINavigationController's UITabBarItem inside a UITabBarController

I'm developing a iphone app that has a main viewcontroller(UITabBarController) with UINavigationControllers in each tab. The problem is that I can't change the TabBarItem's data(title and image).
This is how I load the viewcontrollers:
AroundViewController *aroundViewController = [[AroundViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aroundViewController];
[[navController navigationBar] setTintColor:[UIColor grayColor]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
[self presentViewController:tabBarController animated:YES completion:nil];
I'm trying set the tabbaritem in the aroundviewcontroller's init, but I don't succeed.
I was reading and one solution was, in the init,
[self setTitle:#"whatever"];
This worked but I couldn't change the image.
Thanks in advance.
Use the tabBarItem instance of your view controller to access the tab bar item directly!
[self.tabBarItem ....]
[self.tabBarItem setImage: ...]
[self.tabBarItem setTitle: ...]
EDIT:
hm, try the following then in your init or viewWillAppear method:
UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:#"YourTitle" image:[UIImage imageNamed:#"YourImage.png"] tag:9];
self.tabBarItem = tabBarItem;
[tabBarItem release];
Before setting the UIViewControllers for the UITabBarController you can do this:
[navController.tabBarItem setImage:[UIImage imageNamed:#"MY-IMAGE"]];
[navController setTitle:#"MY-TITLE"];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
I found a solution.
In the viewWillAppear I put:
[[[self navigationController] tabBarItem] setTitle:#"HELLO"];
That works.

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.

How to Give action to the tab bar item

This is my code for my tab bar
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Crops ", #"Articles");
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 376, 320, 44)];
item1 = [[UITabBarItem alloc] initWithTitle:#"TypesToTry" image:[UIImage imageNamed:#"crops.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"WhenToPlant" image:[UIImage imageNamed:#"crops.png"] tag:1];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4,item5, nil];
[tabBar setItems:items animated:YES];
[tabBar setSelectedItem:nil];
tabBar.delegate=self;
[self.view addSubview:tabBar];
}
what i want is whenever i tap on the 1st tab i want to go to that particular description ,so where to right this action.Please help me in this.
The questions may be simple,but for me they are difficult :),Please help this new developer
Thanks
Hi i think you can add following line of code to make your tab bar item workable
[item1 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
[item2 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
put this line at end of -(void) didLoad, i hope it will work for you

Setting custom UITabBarItem programmatically?

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.

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];