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

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.

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.

iOS 7 UITabBar badge position

Is there any way to adjust the position of the UITabBar badge in iOS 7? The badge now blocks the tab bar icon a bit more than I would like.
iOS 6:
iOS 7:
It looks like the badge is placed in a certain position relative to the image. So if you have no image, the badge is in the upper left corner of the tabBarItem.
So - to position the badge, adjust the border of blank pixels around the .png you're using for the tabBarItem image.
If possible, can you provide the method by which you are setting the tab bar image?
I had the same problem that you did, and fixed it by using UIImageRenderingModeAlwaysOriginal:
UIImage *image = // Your tab bar item image
UIImage *selected = // Your selected tab bar item image
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
selected = [selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:title
image:image
selectedImage:selected];
Cheers!
It's not possible to adjust appearance of the badge.
If you really want to have it different, I think implementing custom overlay on UITabBar should be pretty easy. That way you could put there any custom text, not just numbers.
iOS 7 SDK depreciate 3 key method we used to customize tabbar
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
- (UIImage *)finishedUnselectedImage
- (UIImage *)finishedSelectedImage
They suggest their alternatives in docs as #Daniel Amitay suggests.
Documentation is here
https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarItem_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UITabBarItem/setFinishedSelectedImage:withFinishedUnselectedImage:

Badge on TabBar when built by Interface Builder

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

How to set first TabBar selected programmatically on iPhone

I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.
My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make it selected?
Thanks.
This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
In swift if tabbar is used not tabbarcontroller set default select
var tabbar:CustomTabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem
or
let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem
set the
tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.
[self.tabBar setSelectedItem:self.tabBar.items[0]];
or
self.tabBar.selectedItem = self.tabBar.items[0];
The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.
This is wrong then: tabbar.selectedItem=0;
You may select other tabs as well. Happy coding

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