Custom UITabBar icons iOS - ios

I am developing a tabbed application and i want to integrate custom UITabbar background and icons. I already inserted a custom background but i have a problem with the icons.
This is how the icons are supposed to look:
But after adding the icons for each view in the storyboard i get grey icons like this:
Does anyone have an idea why the icons are displayed this way?
Thank you very much!

Using Xcode 6, in order to be able to change images directly from Storyboard, you can do this:
#IBDesignable class CustomizedTabBarItem: UITabBarItem {
#IBInspectable var finalImage:UIImage = UIImage() {
didSet {
self.image = finalImage.imageWithRenderingMode(.AlwaysOriginal)
}
}
#IBInspectable var finalSelectedImage:UIImage = UIImage() {
didSet {
self.selectedImage = finalSelectedImage.imageWithRenderingMode(.AlwaysOriginal)
}
}
}
After, you just have to set the images in your storyboard

I can´t comment yet, so I´ll add as answer.
Since iOS 7 setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated.
To solve this you can do it this way:
UIImage *selectedImage = [UIImage imageNamed:#"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:#"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem = [[UITabBarItem alloc] initWithTitle:#"TabBarItem1" image:unselectedImage selectedImage:selectedImage];
//
//OR
//
UIImage *selectedImage = [UIImage imageNamed:#"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:#"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[yourTabBarItem setImage:unselectedImage];
[yourTabBarItem setSelectedImage:selectedImage];
Hope this help to clarify for iOS7+.

This is how I do it in one of my apps. In your AppDelegates didFinishLaunchingWithOptions: method, add this and fill in your image names:
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"]];
}
Works perfect for me.

An updated Version to Julien's answer about IBInspectable is below
import UIKit
#IBDesignable
class CustomTabBarItem: UITabBarItem {
#IBInspectable var finalImage: UIImage = UIImage() {
didSet {
self.image = finalImage.withRenderingMode(.alwaysOriginal)
}
}
#IBInspectable var finalSelectedImage: UIImage = UIImage() {
didSet {
self.selectedImage = finalSelectedImage.withRenderingMode(.alwaysOriginal)
}
}
}

You can set the finished image of the tab bar icons with setFinishedSelectedImage:withFinishedUnselectedImage: on UITabBarItem
Check the docs here (look under Appearance)
The default behavior of the UITabBarItem is to create a mask based on your icon then it applies that special blue treatment as your seeing.

You can also keep it simple ;)
[tabBarItemName setSelectedImage:[UIImage imageNamed:#"image name"]];
You'll have to declare the tabBarItemName in your ViewController.h

Related

custom image for tabbar item

change a custom image for UITabBar item
I have used this code but, it's not working.
UIImage *selectedImage0 = [UIImage imageNamed:#"f.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"f.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"g.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"g.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"i.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"i.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"s.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"s.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"t.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"t.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];
You should use - (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0); method like below
item0.image = [unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.selectedImage = [selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
You have set two images for each tabitem simple image and hover image.
item0.image = [[UIImage imageNamed:#"f.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // set simple image
item0.selectedImage = [[UIImage imageNamed:#"f.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // set hover image

Tab bar button always show blue color in iOS?

I have added a tabbar in my app. I am showing 5 tabs at once. I am trying to make the custom colors of the tab bar. I have used following in my TabBarController
UITabBarItem *homeTab = [self.exposeTabBar.items objectAtIndex:0];
UIImage *home_unselectedImage = [UIImage imageNamed:#"home.png"];
UIImage *home_selectedImage = [UIImage imageNamed:#"home.png"];
[homeTab setImage: [home_unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[homeTab setSelectedImage: home_selectedImage];
UITabBarItem *bookmarkTab = [self.exposeTabBar.items objectAtIndex:1];
UIImage *bookmark_unselectedImage_ = [UIImage imageNamed:#"bookmark.png"];
UIImage *bookmark_selectedImage = [UIImage imageNamed:#"bookmark.png"];
[bookmarkTab setImage: [bookmark_unselectedImage_ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[bookmarkTab setSelectedImage: bookmark_selectedImage];
UITabBarItem *postTab = [self.exposeTabBar.items objectAtIndex:2];
UIImage *post_unselectedImage_ = [UIImage imageNamed:#"create-post.png"];
UIImage *post_selectedImage = [UIImage imageNamed:#"create-post_white.png"];
[postTab setImage: [post_unselectedImage_ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[postTab setSelectedImage: post_selectedImage];
UITabBarItem *notificationTab = [self.exposeTabBar.items objectAtIndex:3];
UIImage *notification_unselectedImage_ = [UIImage imageNamed:#"notifications.png"];
UIImage *notification_selectedImage = [UIImage imageNamed:#"notifications_white.png"];
[notificationTab setImage: [notification_unselectedImage_ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[notificationTab setSelectedImage: notification_selectedImage];
UITabBarItem *profileTab = [self.exposeTabBar.items objectAtIndex:4];
UIImage *profile_unselectedImage_ = [UIImage imageNamed:#"user-profile.png"];
UIImage *profile_selectedImage = [UIImage imageNamed:#"user-profile_white.png"];
[profileTab setImage: [profile_unselectedImage_ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[profileTab setSelectedImage: profile_selectedImage];
For unselected Image it is showing the correct image but for selected image it is not showing the right image. It is showing some blue image for the icon.
Please tell how can I prevent this?
Try using the tab bar appearance api method with clear color, in app delegate:
[[UITabBar appearance] setTintColor:[UIColor clearColor]];
try to change tabBar color and tabBar tint
example:
// Generate a black tab bar
self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
// Set the selected icons and text tint color
self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
Try to change tabBarController's "tabEdgeColor" and "topEdgeColor".
Or change the barTint or tint color of tabBar to clear color.
For selected image, you should also set rendering mode, try this:
[homeTab setSelectedImage:[home_selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
You can use custom image for different color
tabBarItem1.image = [[UIImage imageNamed:"imag"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.selectedImage = [[UIImage imageNamed:#"selected_image"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

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.

Set background for each tab bar item

I have struggled several hours now trying to add an uniqe image (because i want the tab separators to show up correctly with no separators to the far left and right) for each item in my UITabBar.
The following code I have in my app delegate displays the items in a strange way at the bottom.. Also would want to remove the title labels. What would be the correct approach? This is driving me crazy..
Image of my tabbar
UIImage *selectedImage0 = [UIImage imageNamed:#"home-tab-active"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"home-tab"];
UIImage *selectedImage1 = [UIImage imageNamed:#"guide-tab-active"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"guide-tab"];
UIImage *selectedImage2 = [UIImage imageNamed:#"search-tab-active"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"search-tab"];
UIImage *selectedImage3 = [UIImage imageNamed:#"favorites-tab-active"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"favorites-tab"];
UIImage *selectedImage4 = [UIImage imageNamed:#"locations-tab-active"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"locations-tab"];
// The Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBarItem *item0 = [tabBarController.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBarController.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBarController.tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBarController.tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBarController.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];
The titles and images of tab items should come from the content controllers assigned to each of the tabs, not from the tab bar controller. It looks like you have the ones you're creating in the app delegate as well as ones that belong to the individual controllers, and that's why you're getting overlapping images and titles (the default string for a tab bar item is the title of the controller assigned to that tab).

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

Resources