Setting TabBar Item title and Image on Selection in XCode 6.4 - ios

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/

Related

How to set image on UITabbaritem?

I am working on UITabbarview controller, I set an image with the following code, but it is not visible the first time I open a view.
UIImage *prfimage = [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;
You should use this peace of code to set UIImage in your ViewController
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[[UIImage imageNamed:#"yourImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]] ;//objectAtIndex:0 mean your first TabBar item You can do it for multiple.
It will help.Thanks

Icons displaying in selected state in more tabbar controller IOS8

I am using a tab bar controller in my app, and I have more tab bar view controllers. In iOS 8 icons that are assigned to tabs in more tab bar are always in selected(Blue) state, which worked fine in iOS7?
image = [UIImage imageWithContentsOfFile:imagePath];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:tag];
you can try with following code:
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
[tabBarItem1 setImage:[[UIImage imageNamed:#"tab1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"tab1Selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setTitle:title];
and you can do the same things with other tabs

ViewController turns blank when jumping between tabs IOS

I have uiViewControllers embedded in uiNavigationControllers that are driven by a TabBarController. Majority of the time when I move between tabs everythings fine, but at random times when I jump to the first tab the screen goes blank (black). The tabBar is still visible and I'm able to jump to the other two tabs, but the first one remains blank. This has only happened on my iPhone and not in simulator. My appdelegate looks like below code, any ideas?
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];
tabBarItem1.title = #"Tab 1";
tabBarItem2.title = #"Tab 2";
tabBarItem3.title = #"Tab 3";
[tabBarItem1 setImage:[[UIImage imageNamed:#"tab1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"tab1a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:#"tab3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"tab3a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[UINavigationBar appearance].titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
UINavigationController *firstVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *secondVCnav = [[tabBarController viewControllers] objectAtIndex:1];
UINavigationController *thirdVCnav = [[tabBarController viewControllers] objectAtIndex:2];
MyFirstVC *oneVC = [[firstVCnav viewControllers] objectAtIndex:0];
oneVC.managedObjectContext = self.managedObjectContext;
MySecondVC *twoVC = [[secondVCnav viewControllers] objectAtIndex:0];
twoVC.managedObjectContext = self.managedObjectContext;
MyThirdVC *thirdVC = [[thirdVCnav viewControllers] objectAtIndex:0];
thirdVC.managedObjectContext = self.managedObjectContext;
return YES;
From another forum I received a tip to check if I'm using a nib where the view controller setup is preconfigured.
I did have a nib reference in a viewcontroller since I removed it the issue hasn't occured, but since random/rare issue I'll monitor and update if issue comes back.

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