How to make tab bar icon image small? - ios

I want to make the tab tab bar icon images look little small.I have used 30*30 size images and also 60*60 images.but it still does not work for me.Please guide me how can i get the correct solution.
following is my code
//set the custom images for selected & unselected image for tab bar
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:#"more.png"];
UIImage *profile_selectedImage = [UIImage imageNamed:#"more_white.png"];
[profileTab setImage: [profile_unselectedImage_ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[profileTab setSelectedImage: profile_selectedImage];
self.exposeTabBar.tintColor = [UIColor whiteColor];
self.exposeTabBar.selectedImageTintColor=[UIColor whiteColor];
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateSelected];

Use following like wise code for setting the image of the UITabBar.
UITabBarItem *i0 = [self.tabBar.tabBar.items objectAtIndex:0];
[i0 setImage:[UIImage imageNamed:#"home"]];
It's working on my case.

Try setting imageInsets and play with values until it suits your needs.
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 6, 6, 6);

Related

How to keep tintColor in a UIButton then click on this?

I'm, setting the tintColor to the UIImageView of a UIButton like this:
UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor greenColor];
This code is working, but then Click the UIButton, the color return to the initial color of UIImage.
How could I keep the green color added?
Thanks!
Try like this if help.
UIImage *image = [[UIImage imageNamed:#“imageName.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_yourBtn setImage:image forState:UIControlStateNormal];
[_yourBtn setTintColor:[UIColor greenColor]];

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

iOS 7.1 and Xcode 5.1 issues with UITabBar

I have recently updated Xcode to 5.1 and now have issues with the tab bar. In iOS 7 and Xcode 5.0 i used this code:
// Graph tab icon
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *graphTabIcon = [tabBar.items objectAtIndex:0];
UIImage *noRenderGraph = [[UIImage imageNamed:#"graph"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[graphTabIcon setImage:noRenderGraph];
[graphTabIcon setTitle:#"Graph"];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
// Friends tab icon
UITabBarItem *friendsTabIcon = [tabBar.items objectAtIndex:1];
UIImage *noRenderFriends = [[UIImage imageNamed:#"group"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[friendsTabIcon setImage:noRenderFriends];
[friendsTabIcon setTitle:#"Friends"];
// Settings tab icon
UITabBarItem *settingsTabIcon = [tabBar.items objectAtIndex:2];
UIImage *noRenderSettings = [[UIImage imageNamed:#"settings"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[settingsTabIcon setImage:noRenderSettings];
[settingsTabIcon setTitle:#"Settings"];
// Info tab icon
UITabBarItem *infoTabIcon = [tabBar.items objectAtIndex:3];
UIImage *noRenderinfo = [[UIImage imageNamed:#"info"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoTabIcon setImage:noRenderinfo];
[infoTabIcon setTitle:#"Info"];
and the result was this :
But now with 7.1 and Xcode 5.1 I get this :
I also tried this code :
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
and that looked great on iOS 7.1, but on 7 ended up being a slight brown color.
So how can I make it so it works on both?
Based on Leo Natan's comments I did this :
Edit
I ended up just adding both code snippets together and it works how I want it to
App delegate
{
// iOS 7 method
// Graph tab icon
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *graphTabIcon = [tabBar.items objectAtIndex:0];
UIImage *noRenderGraph = [[UIImage imageNamed:#"graph"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[graphTabIcon setImage:noRenderGraph];
[graphTabIcon setTitle:#"Graph"];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
// Friends tab icon
UITabBarItem *friendsTabIcon = [tabBar.items objectAtIndex:1];
UIImage *noRenderFriends = [[UIImage imageNamed:#"group"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[friendsTabIcon setImage:noRenderFriends];
[friendsTabIcon setTitle:#"Friends"];
// Settings tab icon
UITabBarItem *settingsTabIcon = [tabBar.items objectAtIndex:2];
UIImage *noRenderSettings = [[UIImage imageNamed:#"settings"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[settingsTabIcon setImage:noRenderSettings];
[settingsTabIcon setTitle:#"Settings"];
// Info tab icon
UITabBarItem *infoTabIcon = [tabBar.items objectAtIndex:3];
UIImage *noRenderinfo = [[UIImage imageNamed:#"info"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoTabIcon setImage:noRenderinfo];
[infoTabIcon setTitle:#"Info"];
// iOS 7.1 method
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
}

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

Resources