colour icon in UITabbar item objective-c - ios

I have a UITabbar controller with 3 item, I want to have colour icon instead of based Gary icons,
would you please give me some hint that how can I have colour icon in tababr,
Here is my code:
self.title = #"test";
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage
imageNamed:#"test"] tag:0];
normally test is a icone with a colour picture, but in UITabbar it's just Gary,
Thanks in advance!

Use this code:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"test"]
withFinishedUnselectedImage:[UIImage imageNamed:#"test"]];

You will need to build your own TabBarController. As Per the Apple docs on the matter "This class [UITabBarController] is not intended for subclassing". The docs on the UITabBarItem say that when you are supplying an image for the tab bar "The images displayed on the tab bar are derived from this image". So, whatever image you provide to the tab bar will get manipulated to make it conform to the "normal" look of a tab bar image.
So, you can build a UIViewController with some UIButtons as subviews and then manage the entire look and feel that way.

you have to create 2 images for all 3 tabs one is unselected and second is selected.
after that in your appdelegate.m file write below code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.searchArray = [[NSMutableArray alloc]init];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
viewController1 *view1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
view1.tabBarItem.image = [UIImage imageNamed:#"tab-selected-1"];
view1.tabBarItem.title = #"Title1";
viewController2 *view2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
view2.tabBarItem.image = [UIImage imageNamed:#"tab-selected-2"];
view2.tabBarItem.title = #"Title2";
viewController3 *view3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
view3.tabBarItem.image = [UIImage imageNamed:#"tab-selected-3"];
view3.tabBarItem.title = #"Title3";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view3, nil];
UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-1.png"]];
UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-2.png"]];
UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-3.png"]];
return YES;
}
try this your problem surly solved. Best Of Luck

Related

when i am adding in UITabBar background image then height increase automatically

I am adding UITabBar controller, But problem is that when i am set background image its Black space from bottom and when i remove the background image its works fine .
I am adding in app delegate
-(void)gotoLoginStoryBoard
{
UIStoryboard *storyboard ;
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *login = [storyboard instantiateInitialViewController];
recentsViewController = [[RecentsViewController alloc]
initWithStyle:UITableViewStylePlain];
recentsViewController.phoneCallDelegate = self;
UINavigationController *recentsViewCtrl = [[[UINavigationController alloc]
initWithRootViewController:
recentsViewController]
autorelease];
recentsViewCtrl.navigationBar.barStyle = UIBarStyleBlackOpaque;
[recentsViewController release];
phoneViewController = [[[PhoneViewController alloc]
initWithNibName:nil bundle:nil] autorelease];
phoneViewController.phoneCallDelegate = self;
ContactViewController *contactsViewCtrl = [[[ContactViewController alloc]
init] autorelease];
contactsViewCtrl.phoneCallDelegate = self;
VoicemailController *voicemailController = [[VoicemailController alloc]
initWithStyle:UITableViewStyleGrouped];
voicemailController.phoneCallDelegate = self;
UINavigationController *voicemailNavCtrl = [[[UINavigationController alloc]
initWithRootViewController:
voicemailController]
autorelease];
voicemailNavCtrl.navigationBar.barStyle = UIBarStyleBlackOpaque;
[voicemailController release];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
/*favoritesViewCtrl,*/ login,
phoneViewController, contactsViewCtrl, nil];
tabBarController.selectedIndex = 0;
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbarimage.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"fill3Copy.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"pathCopy4.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setTitle:#"Contact"];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"fill3Copy3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"pathCopy5.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setTitle:#"Group"];
[[UITabBar appearance] setBackgroundColor:[UIColor blackColor ]];
[self.window setRootViewController:tabBarController];
}
First of All check your imagesize.
from apple documentation the maximum size of a picture on the TabBar
are 30x30 (60x60 for the retina display).
I think it's not possible to take the entire height of the TabBar without strech the image like UIimageview . so, according to me the best solution is to center the image in the TabBar using imageInset.
tabBarItem.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
OR
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
Set Imageinsect till image fit to tabbar controller.
For More Detail Check Apple Document below link.
Icon and Image Sizes
I hope it will helpful for you.
i was missing this line in appellate.
MelpHomeViewController *melpHome = (MelpHomeViewController *)[storyboard instantiateViewControllerWithIdentifier:#"homeController"];

Customizing appearance of tabBarItems in a TabBArController

In my application, I am using a UITabBarController. However, i need to customise the appearance of my UITabBarItems.
At present, my screen looks like this:
I would like to increase the distance between the tabs, and the middle tab has bigger text, due to which the tab looks very crowded. Also, i would like to add a separator between the tabs.
Here's the code:
customerCareNavController = [[UINavigationController alloc] initWithRootViewController:custCareVC];
customerCareNavController.title = #"Inquiry";
purchaseOrderNavController = [[UINavigationController alloc] initWithRootViewController:POController];
purchaseOrderNavController.title = #"Purchase Order";
accAndContactsNavController = [[UINavigationController alloc] initWithRootViewController:accAndContactsController];
accAndContactsNavController.title = #"Accounts And Contacts";
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:customerCareNavController,accAndContactsNavController,purchaseOrderNavController, nil];
[(UITabBarItem*)[tabBarController.tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:#"Customer_Service.png"]];
[(UITabBarItem*)[tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:#"Acc_Cont.png"]];
[(UITabBarItem*)[tabBarController.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:#"PO.png"]];
Set tabBarItem such like
UITabBarItem* ClientSupportTabBarItem = [[UITabBarItem alloc] init];
[ClientSupportTabBarItem setFinishedSelectedImage: [UIImage imageNamed: #"btnTabItem-active.png"] withFinishedUnselectedImage: [UIImage imageNamed: #"btnTabItemNormal.png"]];
ClientSupportTabBarItem.title = #"Inquiry";
[customerCareNavController setTabBarItem: ClientSupportTabBarItem];

How to change default ViewController of UITabBarController

I create basic TabBarController with 2 ViewControllers.
It worked but it have bug and i want something about it.
Problem1.
When I run (default ViewController is FirstViewController) name of SecondViewController in TabBar is not appear.
I want following this picture.
Problem2.
In my code default view is FirstViewController.
If I want to set default view is SecondViewController
by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right)
following this picture. How to resolve it.
This is my example code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNVC.navigationBar.barStyle = UIBarStyleBlack;
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
secondNVC.navigationBar.barStyle = UIBarStyleBlack;
UITabBarController *tabController = [[UITabBarController alloc]init];
tabController.navigationItem.hidesBackButton = NO;
tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil];
tabController.navigationController.navigationBarHidden = NO;
self.window.rootViewController = tabController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Thank You. ^^
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#""] withFinishedUnselectedImage:[UIImage imageNamed:#""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:#"1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:#"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:#"2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:#"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:#"3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:#"tab5.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
Problem2. If I want to
set default view is SecondViewController by Same Tab Bar(FirstView Tab
: Left ,and SecondView Tab : Right). How to
resolve it.
Solution:
[self.tabBarController setSelectedIndex:1];
Problem 1:
try to set a title for the view controllers.
Problem 2:
You just set selected index of the tabBarController to 1, that should do it.
Problem1
You haven't set tabbarItem for each navigation controller, I think you should set up a UITabBarItem with title first, then assign it to tabbarItem property to navigation controller.
Problem2
Just as #Kumar says.

NavigationController Pushing TabBarViewController

I have noticed that my GoogleMap stopped working in the sense that it has become inactive after first touch. This got me thinking, so I decided to create a new project with the same code to check to see if it was the code and shows that its not my GoogleMaps code. I think it has to do with how I set up my Navigation Bar and Tab Bar.
This is what I have. I have an initial ViewController inside a NavigationController. This view controller does many things, one of which is push view controllers. I have a button that pushes a UITabBarController onto the NavigationController stack. I have created a UITabBarController inside storyboard and gave that controller a StoryboardID: GRxTabBarViewController. GRxTabBarViewController has 4 view controllers, PricesViewController, SavingsViewController, InfoViewController, and MapsViewController.
I then created the header and implementation files of these controllers. This is what I have in my initial view controller to push this tab bar controller and tab bars.
-(void)pushTabBar
{
UIStoryboard *iPhoneStoryBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
GRxTabBarViewController *tabViewController = (GRxTabBarViewController*) [iPhoneStoryBoard instantiateViewControllerWithIdentifier:#"GRxTabBarViewController"];
PricesViewController *pricesController = [tabViewController.viewControllers objectAtIndex:0];
SavingsViewController *savingsController = [tabViewController.viewControllers objectAtIndex:1];
InfoViewController *infoController = [tabViewController.viewControllers objectAtIndex:2];
MapsViewController *mapController = [tabViewController.viewControllers objectAtIndex:3];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:infoController];
UINavigationController *savingsNav = [[UINavigationController alloc] initWithRootViewController:savingsController];
UITabBarItem *tabBarItem1 = [[tabViewController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-prices-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-prices-normal"]];
UITabBarItem *tabBarItem2 = [[tabViewController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-savings-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-savings-normal"]];
UITabBarItem *tabBarItem3 = [[tabViewController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-info-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-info-normal"]];
UITabBarItem *tabBarItem4 = [[tabViewController.tabBar items] objectAtIndex:3];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-map-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-map-normal"]];
[tabViewController setViewControllers:[NSArray arrayWithObjects:pricesController, savingsNav, navController, mapController, nil]];
[self.navigationController pushViewController:tabViewController animated:YES];
}
GRxTabBarViewController is a UITabBarController and has this in the implementation file.
GRxTabBarViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *backButton = [[UIButton alloc] init];
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(dismissTab) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = CGRectMake(0, 0, 20.0f, 44.0f);
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
self.navigationItem.hidesBackButton = YES;
}
-(void)dismissTab
{
[self.navigationController popViewControllerAnimated:YES];
}
Am I doing anything wrong? Am I setting up my tab bar incorrectly? Has someone pushed a tab bar before and have any ideas what I am doing wrong that can help me out? Any guidance or help is appreciated. Thanks in advance!

How to add UITabBar with one UITabbarItem work as button?

I want something like looking in image, I want to do this in tabbar application or mainstoryboard. This i did normal single view based application with tabbar item.
I try following code also, in this when i click tabbar items particular viewcontroller opens. But i want on click of third tabbar item it should show UIActionSheet not third viewcontroller. How i can achieve this.
FisrtTabViewController *first = [[FisrtTabViewController alloc]initWithNibName:#"FisrtTabViewController" bundle:nil];
[first.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"myarea_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"myarea.png"]];
UINavigationController *tabItem1 = [[UINavigationController alloc] initWithRootViewController: first];
SecconTabViewController *second = [[SecconTabViewController alloc] initWithNibName:#"SecconTabViewController" bundle:nil];
[ second.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"areaconv_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"areaconv.png"]];
ThirdTabViewController *third = [[ThirdTabViewController alloc] initWithNibName:#"ThirdTabViewController" bundle:nil];
[ third.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"share_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"share.png"]];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1,second,third,nil];
[[UITabBar appearance]setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
tabbar1.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;
[self presentViewController:tabbar1 animated:YES completion:NULL];
Thanks in advance.

Resources