iOS 7.1 and Xcode 5.1 issues with UITabBar - ios

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

Related

NSRangeException indexOutOfBound

I am using code that I made in another application. In the other application the code is working fine. What I do is while loading splashViewController the images of slideShow are being downloaded and cached. When the download is finished it will present the mainviewcontroller which is a maintableviewcontroller. Here is the code that I am using:
UIImage* tabBarBackground = [UIImage imageNamed:#"tabBar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"barController"];
UITabBar *tabBar = rootViewController.tabBar;
UITabBarItem *tabBarItem1 = tabBar.items[0];// the error here happening event if i comment out this line the error is hapen at the next uibar items
UIImage *selectedLogo = [[UIImage imageNamed:#"productSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedLogo = [[UIImage imageNamed:#"product"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem1 setImage:unselectedLogo];
[tabBarItem1 setSelectedImage:selectedLogo];
[tabBarItem1 setTitle:#"My Product"];
UITabBarItem *tabBarItem2 = tabBar.items[1];
UIImage *selectednews = [[UIImage imageNamed:#"notificationSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectednews = [[UIImage imageNamed:#"notification"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem2 setImage:unselectednews];
[tabBarItem2 setSelectedImage:selectednews];
[tabBarItem2 setTitle:#"Notifications"];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UIImage *selectedLocation = [[UIImage imageNamed:#"locationSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedLocation = [[UIImage imageNamed:#"location"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem3 setImage:unselectedLocation];
[tabBarItem3 setSelectedImage:selectedLocation];
[tabBarItem3 setTitle:#"Locate Us"];
UITabBarItem *tabBarItem4 = tabBar.items[3];
UIImage *selectedaboutus = [[UIImage imageNamed:#"moreSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedaboutus = [[UIImage imageNamed:#"more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem4 setImage:unselectedaboutus];
[tabBarItem4 setSelectedImage:selectedaboutus];
[tabBarItem4 setTitle:#"More"];
[self.loadingActivityIndicator stopAnimating];
[self.navigationController popToRootViewControllerAnimated:YES];
[self presentViewController:rootViewController animated:YES completion:nil];
Please put a count check for tabbar.items first and then access all tab bar items :
if(tabbar.items.count==tabCount) //tabCount is number of tabs in your tabbar
{
UITabBarItem *tabBarItem1 = tabBar.items[0];
//<remaining code>
}else
{
//either tabbar is nil or tabbar doesn't not have any tabs, so handle accordingly
}
Also, since the tabbar is working fine in another application, please check your StoryBoard if it correctly creates and binds the tabbar for the rootViewController.
Here is a good post with detailed steps to create tabbar correctly using Storyboards which can help you debug/verify if you created the bindings and segues correctly:
https://guides.codepath.com/ios/Using-Tab-Bar-Controllers

How to set image in uitabbaritem for default

I am working in UITabBarController, when I am going to set image for tabs, the image does not appear for first time when view is open. I don't know how to resolve this problem.
This is the code I am using:
UIImage *prfimage = [UIImage imageNamed:#"profile_o.png"];
UIImage *selimage = [UIImage imageNamed:#"profile_o.png"];
UITabBarItem *tbP = [[UITabBarItem alloc] initWithTitle:#"" image:prfimage tag:YES];
[self setTabBarItem:tbP];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:247/255.0f green:148/255.0f blue:29/255.0f alpha:2.0f]];
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.title = nil;
by following way you can set.
UITabBarItem *tabItem = controller.tabBarItem;
tabItem.image = [[UIImage imageNamed:#"history"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

How to make tab bar icon image small?

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

Setting TabBar Item title and Image on Selection in XCode 6.4

I am using xcode 6.4 and using storyboard to set up Tab bar controller Item and its Images.
Issue 1- In storyboard on tab bar item selection, there are already option for Bar Item and Image selection. i have assigned Images on both field but Images are not changing at time of selection.Only default(black) image exists.
I have referred to this Link . Then also default (black) exists
Issue 2- In storyboard i have assigned all images on Bar Item and Image Selection and used given code then title is not visible in tab bar item. only image is working fine on selection.
Code-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Search";
tabBarItem3.title = #"Cart";
tabBarItem4.title = #"Account";
tabBarItem5.title = #"More";
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"HomeSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"SearchOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"CartOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setSelectedImage:[[UIImage imageNamed:#"ProfileOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem5 setSelectedImage:[[UIImage imageNamed:#"MoreTabOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:0.0/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateHighlighted];
return YES;
}
I've seen gone through iOS Docs, but could not find any useful update on these issues. please update me on it.
You should create your custom tabbar
Useful link : http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/

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.

Resources