Badge on TabBar when built by Interface Builder - ios

Is it possible to programmable set a badge value on a TabBar which is made in Interface Builder (Xcode4). I have four tabs and I would like to have a badge on the forth tab if there is unread items in the inbox that is behind the tab.
Or do I have to rebuild the tabbar by code to be able to do this?
Thanks for any thoughts on this.

You can set it as -
UITabBarItem *tbi = (UITabBarItem*)[[[self.tabBarController tabBar] items] objectAtIndex:3];
[tbi setBadgeValue:#"1"];

Related

How to Add TabBar Images to each tab? on XCode 8.1

I am using Xcode 8.1, on that when I uses TabBarViewController, it doesn't shows separate buttons for each tab, I want to add each Tab's specific image and title.
Following is the screenShot. My Problem is very Simple, I only want to Add Icons and titles to each Tab.
I am beginner.
Any Idea will be Appreciated.
I'm using XCode 8.2.1, but the procedure should be similar to XCode 8.1
Steps to add image into tab
1. In your storyboard, drag a TabBarController from the palette, it comes with 2 default ViewController
Highlight one of the Default ViewController
Select item1 as shown in this image Item1 Sample
Switch to Attributes Inspector Tab as shown in this image Attributes Inspector Sample
Then change the value of Title and Image to what you want.
You're done.
In case You want to know how to manually link those ViewController together.
Drag one TabBarController from the palette.
Remove Both Default ViewController come along with TabBarController.
Drag one ViewController from the palette.
Control+Drag from TabBarController to ViewController.
Select 'view controller' under Relationship Segue.
Then Follow the steps in upper section to add your image and title of your tab bar item
If I understand correctly and you are trying to customize the button icons in the tapbar, then you can do it programmatically using this code for each tapbar button index:
UITabBarController *myTabBar = [[UITabBarController alloc] init];
UITabBarItem *tabButton1 = [myTabBar.items objectAtIndex:0];
[tabButton1 setSelectedImage:[UIImage imageNamed:#"selected_image"]];
tabButton1.selectedImage = [[UIImage imageNamed:#"selected_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabButton1.image = [[UIImage imageNamed:#"unselected_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabButton1.title = #"Button 1 title";
NSArray *arrayViewControllers = [NSArray viewController1, viewController2, viewController3, nil];
[myTabBar setViewControllers:arrayViewControllers animated:NO];
Alternatively, you can set the image in the storyboard but keep in mind that the image alpha channel will be used to generate the select/unselect color tonality. That sometime lead to strange effects.

is it possible to add UILabel inside Tab Bar Item (BadgeValue)

Referring above image:
1. The issue is to set Number inside circle of Tasks image in "Tasks tab".
2. Is it possible to set number i.e UILabel at given location inside tab bar item?
Thank You.
Updated:
This is what i wanted and got using badge value:
[[[[[self tabBarController] tabBar] items]
objectAtIndex:1] setBadgeValue:10 ]];
To get badge value to application value:
[UIApplication sharedApplication].applicationIconBadgeNumber = 10;
That design element is called a 'badge'. You set a tab bar item's badge by setting its badgeValue property to a suitable number.
Check the docs for UITabBarItem for more information on the badgeValue property.

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.

how can i set the badge in tab bar in objective-c?

I want to put the notification in the tab bar of the app to show the number of item selected for the shopping list.
Is there any way to add the notification in the tab bar of app.
You mean the badge? Do something like (where self is a UIViewController):
[[self tabBarItem] setBadgeValue:#"42"];
On the storyboard you have to select your navigation view controller if you have and then open your Property window and set Badge according to your requirement.
if you want to set programmatically
1. find your tabbar Item by viewcontroller
# badge=what you want to set.
2.[[[[self tabBarController] tabBar] items] objectAtIndex:1] setBadgeValue:badge];
and if you want to set by storyboard please refer this image.
Hope this will helps.
Thanks
Also if you want to use in swift. Below code will work for you
[[self tabBarItem] setBadgeValue:#"42"];
You can set the badgeValue property on a UITabBarItem to present the user with a small red badge on that item.
I did it using:
self.navigationController.tabBarItem.badgeValue = #"2";

Resources