Tabbar item image doesn't appear - ios

I have an issue with the images of tabbar items. I set an image on each tabbar item but when i run the app, the only image that appears is that of the first tabbar item. Images of others tabbar items don't appear until i select one of these tabs. The code that i use to set an image on a tabbar item is the following:
[self.tabBarItem setImage:[UIImage imageNamed:#"multi30x30.png"]];
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"multi30x30.png"]
withFinishedUnselectedImage:[UIImage imageNamed:#"multi30x30.png"]];
Can someone help me?

You can try this:
[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"LocateIconInactive.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ProductsIconInactive.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"NextDeliveryIconInactive.png"]];

[[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"blabla1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"bleble1.png"]];
[[tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"blabla2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"bleble2.png"]];
[[tabBarController.tabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#"blabla3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"bleble3.png"]];

use this code :--
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
UITabBarController *tabBarController
= (UITabBarController
*)self.window.rootViewController;
UIImage *selectedImage0 = [UIImage
imageNamed:#"tb_inorganic_selected"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"tb_inorganic_normal"];
UIImage *selectedImage1 = [UIImage imageNamed:#"tb_organic_selected"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"tb_organic_normal"];
UIImage *selectedImage2 = [UIImage imageNamed:#"tb_acid_selected"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"tb_acid_normal"];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];

Creates and returns a new item with the specified title, unselected image, and selected image.
If no selectedImage is provided, image is used as both the unselected and selected image.
By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal. Availability iOS 7 and later.
UIImage *aImage = [UIImage imageNamed:#"a"];
aImage = [aImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *inaImage = [UIImage imageNamed:#"ina"];
inaImage = [inaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.aController.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"title" image:inaImage selectedImage:aImage];

I would prefer that the ViewController do this. So, in my opinion, the best solution should be override init method and set the TabBarItem here.
Probably you are doing it in viewDidLoad and so only the first item appear.
Sorry for the spelling mistakes.

SWIFT 5.4
tabBarController?.viewControllers?[index].tabBarItem.image = UIImage(named: "imageName")

Related

UITabBar custom icon color is not getting shown

I am using UITabBar in iOS app. I have set images to UITabBar items. Code is-
self.mainTabBar.delegate = self;
mainTabBar.tintColor = nil;
[[mainTabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"ic_1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_1.png"]];
[[mainTabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"ic_2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_2.png"]];
[[mainTabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#"ic_3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_3.png"]];
[[mainTabBar.items objectAtIndex:3] setFinishedSelectedImage:[UIImage imageNamed:#"ic_4.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_4.png"]];
[[mainTabBar.items objectAtIndex:4] setFinishedSelectedImage:[UIImage imageNamed:#"ic_5.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_5.png"]];
This is code for tab bar. It is showing like -
Actual icon image is like (colors are like this)-
How can I show actual colors of icon?
You can use it like this if you are using iOS 8.
UIImage *image1 = [[UIImage imageNamed:#"message"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.image = image1;
tabBarItem1.selectedImage = image11;
Here is the source : UITabBarItem/selectedImage
By default, the actual selected image is automatically created from
the alpha values in the source image. To prevent system coloring,
provide images with UIImageRenderingModeAlwaysOriginal.
Set the UITabBarItems from inside their view controllers. Do not set them directly.
For example:
UITableViewController *searchTableCon = [[UITableViewController alloc]init];
UINavigationController *searchNavCon = [[UINavigationController alloc]initWithRootViewController:searchTableCon];
searchTableCon.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Search"
image:[UIImage imageNamed:#"Search"]
selectedImage:[UIImage imageNamed:#"Search"]];
You will have to do following thing for this.
UIImage * selectedImage = [UIImage imageNamed:#"selected_image.png"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewCtrl.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Title" image:selectedImage selectedImage:selectedImage];

Customize UITabBarItem in UINavigationController

How can I change background color of UITabBarItem? I need something like this:
(color under wallet)
To add an image
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
//tab1
UIImage *selectedImage = [UIImage imageNamed:#"home-tab-selected"];
UIImage *unselectedImage = [UIImage imageNamed:#"home2-tab"];
MyTabBar *tabBar = (MyTabBar *)tabController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab2
selectedImage = [UIImage imageNamed:#"customers-tab-selected"];
unselectedImage = [UIImage imageNamed:#"customers-tab"];
UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
[item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab3
selectedImage = [UIImage imageNamed:#"maps-tab-selected"];
unselectedImage = [UIImage imageNamed:#"maps-tab"];
UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
[item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab4
selectedImage = [UIImage imageNamed:#"reports-tab-selected"];
unselectedImage = [UIImage imageNamed:#"reports-tab"];
UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
[item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
//tab5
selectedImage = [UIImage imageNamed:#"orders-tab-selected"];
unselectedImage = [UIImage imageNamed:#"orders-tab"];
UITabBarItem *item5 = [tabBar.items objectAtIndex:4];
[item5 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:#"tab-bg"]];
}
Take a look at the appearance proxy of UITabbarItem, particularly for the method -selectedImage(doc).
Just pay attention that in iOS7 most of those images are rendered as templates (I don't know in tab bar item) so you should specify their rendering mode.
Something like that:
[[UIImage imageNamed:#"wallet"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Those methods are safe on iOS7, if you need to deploy on lower target is better that you check if they are available or your app will crash.

How to set the images for UITabBarItems without having titles?

I had an application in which I am adding the images for the UITabBarItems programatically like this..
UIImage *selectedImage0 = [UIImage imageNamed:#"home_hvr.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"home.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"star_hvr.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"star.png"];
UITabBar *tabBar = self.tabbarcontroller.tabBar;
self.tabbarcontroller.tabBar.barStyle = UIBarStyleBlack;
self.tabbarcontroller.tabBar.translucent = NO;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
[item0 setSelectedImage:[selectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setImage:[unselectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setSelectedImage:[selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImage:[unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
since the tabbaritems doesn't have any titles it will bring a blank space at the bottom of the tab-bar pulling the image above the tab bar,I need no titles but needs to put my image inside the tab bar completely.
Can any body guide me on this?
UITabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
was done this trick worked for me .thanks
Try below code:
[[self.tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"offer_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"offer_btn.png"]];
[[self.tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"merchant_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"merchant_btn.png"]];

iOS Custom Tab Bar Items Taller Than Tab Bar

Ok, so, I'm trying to create a custom tab bar. I've read a hundred tutorials and downloaded half as many sample code projects and this problem seems to still be in all of them.
I don't really want a custom background, what I really want to for the buttons to take up the entire area so that no background ever shows. Why is it that when using an image sized at 49px (98px for Retina) for the background AND the buttons...the tab bar itself is scaled up and that ugly glow that Apple adds is viewable?
Here's the code I used (found here on Stack):
UIImage *selectedImage0 = [UIImage imageNamed:#"transactions_tab_btn_active"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"transactions_tab_btn"];
UIImage *selectedImage1 = [UIImage imageNamed:#"bills_tab_btn_active"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"bills_tab_btn"];
UIImage *selectedImage2 = [UIImage imageNamed:#"messages_tab_btn_active"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"messages_tab_btn"];
UIImage *selectedImage3 = [UIImage imageNamed:#"reports_tab_btn_active"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"reports_tab_btn"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UIImageView *tabBarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_bar"]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 5) {
[tabBar insertSubview:tabBarImageView atIndex:0];
}else{
[tabBar insertSubview:tabBarImageView atIndex:1];
}
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
Again, I can remove that background image as it's of no use to me, but without it, the default black background for the tab bar is viewable. Both bad. Help.Please.
Well, I can help you with the glow. To hide it just create a transparent image and use it instead.
UIImage* image = [UIImage imageNamed:#"transparent.png"];
[[UITabBar appearance] setSelectionIndicatorImage:image];

Creating custom tabs in IOS?

I am trying to create custom-tabs in my IOS app. I have followed this simple guide http://kurrytran.blogspot.se/2011/10/ios-5-tutorial-creating-custom-tab-bar.html Each tab should have 2 background pictures, one when being selected and one being unselected. My porblem is, in my real project I also have navigation controllers between my Tab bar controller and view controllers which seems to mess everything up. Simply nothing happens, all my tabs are black without any content in them. What shall I do to make it work ?
This is put in firstviewCotroller.m
- (void)viewDidLoad
{
UIImage *selectedImage0 = [UIImage imageNamed:#"HomeDB.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"HomeLB.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"ScheduleDB.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"ScheduleLB.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"BuildingsDB.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"BuildingsLB.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"InformationDB.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"InformationLB.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"MoreDB.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"MoreLB.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
[super viewDidLoad];
}
Would it not be better to put the UINavigationControllers in each tab separately within your MainWindow.xib then in your App delegate use your above code to change the tab bar items in your App Delegate.
I'm doing the following in the "didFinishLaunchingWithOptions" method in my App Delegate.
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"tab-1-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-1.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-3-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-3.png"]];
Thanx for your answer AndyDev! This is my code below....
My AppleDelegate.h file:
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
My AppleDelegate.m file:
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize tabBarController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item4 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
return YES;
}
I am probably doing something wrong. I never tried to customized anything before in IOS and this is my first project I am developing in IOS. Do I need any code in each ViewController.m file aswell? Nothing happens atleast with this code....

Resources