How to change the tabbar selected image using storyboard - ios

I have created application using storyboard and has TabBarController with 5 tabs.
Each tab has tabicon and tab title. When a tab is selected I want to change the tabbar icon.
How can I do using storyboard?

- (void)setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated. If you're using storyboards, it's as simple as
UITabBarItem *tabBarItem0 = [self.tabBar.items objectAtIndex:0];
UIImage* selectedImage = [[UIImage imageNamed:#"settings-active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem0.selectedImage = selectedImage;
EDIT
In Swift:
var settingsItem = self.tabBar.items?[0] as UITabBarItem
settingsItem.selectedImage = UIImage(named: "home-selected")
Note that this code belongs in the viewDidLoad override of your UITabBarController subclass.

I have got it.
Subclass UITabBarController - MyTabBarController
Over write viewDid load :
write
UITabBarItem *tabBarItem0 = [self.tabBar.items objectAtIndex:0];
[tabBarItem0 setFinishedSelectedImage:[UIImage imageNamed:#"selectedimage.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"image.png"]];
like this set for all the tabbar items and in story board set the tabBar controller to MyTabBarController. It's working fine.

You can now do this easily in storyboard. On each tabviewcontroller that you have, it should contain a Tab Bar Item in the hierarchy (looks like a little blue star), Click on this and the settings on the right should look like the image below. The tab bar title & image can be changed here.

Below code will change tabbar image in selection:
UITabBarItem *tabBarItem = [[tabbar items] objectAtIndex:0];
[tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"img_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"img.png"]];
change identifier to custom and add image

Related

Disable tint color on iOS 8 More TabBarController

I am creating the UITabBarItem for my ViewControllers contained in the TabBarController with the new recommended method
UIImage * normal = [UIImage imageNamed:viewIconKey];
normal = [normal imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIImage * selected = [UIImage imageNamed:[NSString stringWithFormat:#"%#On",viewIconKey]];
selected = [selected imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title
image:normal
selectedImage:selected];
of creating my images with UIImageRenderingModeAlwaysOriginal to keep the blue tint from applying.
This works fine on the normal Tab Bar Items, but when I select the "More" tab (I have 9 items in the viewControllers collection), then the icon displayed next to each item is tinted in the "blue" color.
On iOS 7 this does not happen, the icons retain their original color.
Any ideas? New API in iOS 8 for this? The More Tab Controller has always been a PITA.
UITabBarController exposes the MoreTab as a navigation controller called moreNavigationController. You can customize it as explained in this answer.
Hope this helps

Button in the center of a tab bar iOS xcode

I think my title is what i am looking for. So, i have a tab bar with 4 view controllers. I started to configure my app within storyboard and i configured my tab bar controller from there. I, also, have declared the names of the view controllers in my appdelegate file. My question is the following. Can i add a button in the middle of my tab bar controller? I searched a lot about that and i didn't find a complete solution. Can i use storyboard to connect my view controllers to the tab bar and also app delegate to declare that button to the center of tab bar? Please help. This is my code so far in app delegate file (anything else is in storyboard, juts the view controllers connected to tabbarcontroller) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = #"Tab1";
tabBarItem2.title = #"Tab2";
tabBarItem3.title = #"Tab3";
tabBarItem4.title = #"Tab4";
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"maps.png"]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"TAPBARBGR.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], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateHighlighted];
return YES;
}
Thank you.
If you connect 4 pages to the standard UITabbarController it will layout 4 buttons equally dividing the space between them. You don't have any control how they are laid down. You can rearrange them in the storyboard or in the code but with 4 buttons - no button will be in the middle.
You can add 5th button and rearrange it to the 3rd position, so this one will be in the middle of tabbar. Or you can look for 3rd party custom tabbar implementations - there are bunch of them on gitnub and other places. Some of them would have specific middle page that styled differently from others.
Can i add a button in the middle of my tab bar controller?
No.
Assuming that you mean adding a button different from the other tab bar items, then no, UITabBar doesn't provide for this. As explained in the UITabBar documentation, all items in a tab bar are expected to be tab bar items, but you could consider a UIToolbar instead:
Each button in a tab bar is called a tab bar item and is an instance
of the UITabBarItem class. If you instead want to give the user a bar
of buttons that each perform an action, use a UIToolbar object.
From a user experience point of view (IMO) it's probably not a good plan to try to mix tab bar and tool bar functionality.

changing tabbar images when tabbar not root view

I am looking at changing the graphics on a tabbar, such as
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
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];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Maps";
tabBarItem3.title = #"My Plan";
tabBarItem4.title = #"Settings";
The problem I have here is that my tabbarcontroller is not my root view, so how can I reference the tabbarcontroller to change the tab images?
I am following a suggestion from this post (Can I have more than 1 UITabBarController?) that refers to having a tableview that links to one or more tabbarcontrollers.
So my root view is not a tab bar, but the tab bar view is loaded after coming from one previous screen.
I have this all working, with the initial screen and then the tab bar, and everything is working fine, I just need to change the graphics on the tab bar, and am not able to do this as all tutorials on changing tab bar graphics use the app delegate and refer to the tabbarcontroller as the root view.
Any help on this greatly appreciated!
Tab bar items belong to the individual content view controllers in each tab, so rather than trying to reference the tab bar controller, you should change the tab item properties in those controllers. If you want those changes to be visible as soon as the tab bar controller comes on screen, you need to put those customizations in their awakeFromNib or initWithCoder methods (for controllers set up in IB).
Richard,
Firstly, the post you reference pointed out that this is very bad visual design, but anyway, if you need to do it.
How you reference the tab bar controller depends on how you instantiate it. This stuff should be fairly obvious, so I'm guessing that you're working with xibs or storyboards.
Either way, when you load the second view that is going to have the tab bar on it, you'll be loading a view controller to control the view. Just bind the tab bar to a property of the view controller in IB, and you can do your set up in the view did load of the view controller.
Actually, if you're using IB ( XIB or storyboards), you can just set up your icons there.

How to link a tabBarItem to a viewController programmatically (iPhone, iOS)

I have an application with a login system. I want the tabBarController to change dynamically at runtime if the user login himself successfully! I have 5 tabs (Accueil, Tous les Voyants, Inscription, Connexion, Aide).
When the user hit the login button, I want to change Inscription to Achat Jetons and Connexion to Profile and link another ViewController to both of theses tabBarItems!
Right now, I've successfully managed to replace the title and image logo of my tab bar. But I don't know how to link the viewControllers to them! Here's what I got right now:
- (IBAction)BTN_ConnexionClick:(id)sender {
UITabBarController *tabBarController = (UITabBarController *)self.tabBarController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:3];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"menu_iOS_achat.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"menu_iOS_achat.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"menu_iOS_profile.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"menu_iOS_profile.png"]];
tabBarItem1.title = #"Achat Jetons";
tabBarItem2.title = #"Profile";
}
I have created 2 new viewControllers via StoryBoard IB, I just don't know how to replace old linked viewController with new ones! Thanks for your help! :)
The mistake you're making is that you're changing the tab bar controller's tab bar's tab bar items directly. Don't! Change the tab bar controller's view controllers. The tab bar controller gets its tab bar items from them.
You might want to read my book on this topic:
http://www.apeth.com/iOSBook/ch19.html#_configuring_a_tab_bar_controller
Notice especially:
The tab bar controller’s tab bar will automatically display the tabBarItem of each child view controller
Do not do anything to mess that up! (You are messing it up.) Manipulate a view controller's tabBarItem. Manipulate a tab bar controller's viewControllers. Do not touch a tab bar controller's tab bar yourself.

xcode tab controller with customized graphics that won't load until first clicked

I am customizing my TabBar Icons and I'm having a problem with the customized tab graphics not showing up until the first time they are clicked on, except for the first tab. The setup is as follows Entry->VC->Tab Controller->TabVC1->TabVC2->TabVC3->TabVC4->TabVC5. What is the best way to get them showing up on first initialization of the tab controller.
** These attributes are set when I enter the tab controller (they work fine)
// Set background to white for the tab bar
UIImage *tabBackground = [[UIImage imageNamed:#"tabback.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
// setting the selected color to blue
self.tabBar.tintColor = [UIColor blueColor];
// changing the tab bar text color
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,[UIFont fontWithName:#"Copperplate-Bold" size:0.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
** these are set in the individual TabVCs (they show up only after the first time their tab has been clicked)
// loading the custom icon for front and back
UITabBarItem *tabicon = [[UITabBarItem alloc] initWithTitle:#"Daily" image:[UIImage imageNamed:#"Day.png"] tag:0];
[tabicon setFinishedSelectedImage:[UIImage imageNamed:#"Day.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"Day.png"]];
[self setTabBarItem:tabicon];
First of all your tab bar controller should be your root view controller. That is, the rootViewController of your window.
From the documentation:
When deploying a tab bar interface, you must install this view as the
root of your window. Unlike other view controllers, a tab bar
interface should never be installed as a child of another view
controller.
Also, since you call self.tabBar I get the feeling you are subclassing UITabBarController.
The documentation advice against this as well.
That being said, you can set up the tab bar items where ever you do your first appearance set up (the first code snippet).
For instance to alter the second view controllers tab item you would do
// loading the custom icon for front and back
UITabBarItem *tabicon = [[UITabBarItem alloc] initWithTitle:#"Daily" image:[UIImage imageNamed:#"Day.png"] tag:0];
[tabicon setFinishedSelectedImage:[UIImage imageNamed:#"Day.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"Day.png"]];
UIViewController *second = [self.viewControllers objectAtIndex:1];
[second setTabBarItem:tabicon];
The point is that you need to set up the custom appearance before the tab bar view has appeared.
Set the custom tabBarItem in your designated init method. This way it will get set when the controller is initialized and you wont have to wait until it is displayed for the first time to see your custom tabBarItem.
For example, here is an example in Swift, and where the controller is setup in a storyboard:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.tabBarItem = getTabBarItem(NSLocalizedString("Calendar", comment: ""), UIImage(named: "851-calendar.png")!)
}
Note that getTabBarItem() is a custom function of mine that returns a UITabBarItem.

Resources