Full Source code: https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification
Problem:
When the Instagram app wants to let you know that you have new comments, likes or followers, it doesn’t use standard badge values on tab bar items:
Instead it uses a custom tab bar notification view. How do we build a similar custom notification view?
Solution:
Just like in our recipe for the Twitter app’s current tab bar indicator we will add a view on top of the tab bar to notify the user. Instead of a static image, we will add a slightly more complicated view. This view has the background, icons for each of the activities (comments, likes, followers), and labels with the number value of each of these activities. Here is what it looks like laid out in the NIB:
Vertical/Horizontal Location
Again we need to figure out the horizontal and vertical location of our custom view and we essentially do the same thing we did in our Twitter tab bar indicator recipe:
To calculate the vertical location, we start at the top of the tab bar (0), go up by the height of the notification view, then go up another 2 pixels so our view is slightly above the tab bar
For the horizontal location we divide the width of the tab bar by the number of items to calculate the width of a single item. We then multiply the index by the width of single item and add half the width of an item to land in the middle.
Showing/Hiding the custom notification view
In a real app we would hit a web service, get the data and then notify the user by showing the custom notification UI.
In the sample app we have a couple of buttons that show and hide the notification view. We also do a very simple fade in/fade out animation of the notification view.
Showing the custom notification view:
- (void) showNotificationViewFor:(NSUInteger)tabIndex { // To get the vertical location we start at the top of the tab bar (0), go up by the height of the notification view, then go up another 2 pixels so our view is slightly above the tab bar CGFloat verticalLocation = - notificationView.frame.size.height - 2.0; notificationView.frame = CGRectMake([self horizontalLocationFor:tabIndex], verticalLocation, notificationView.frame.size.width, notificationView.frame.size.height); if (!notificationView.superview) [self.tabBar addSubview:notificationView]; notificationView.alpha = 0.0; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; notificationView.alpha = 1.0; [UIView commitAnimations]; }
Hiding the custom notification view:
- (IBAction)hideNotificationView:(id)sender { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; notificationView.alpha = 0.0; [UIView commitAnimations]; }
In the end the solution is quite simple: A custom view laid out in a NIB, added to the view hierarchy at the right location. But it definitely looks sexier than the built in red badge.
What do you think? Can you make this code better? Let us know in the comments!
Full Source code: https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification
Thanks for sharing this with us. This is really a good idea. I could draw that view just with code. I prefer to do everything by code like drawing this notification view instead of using graphics. So i have a dumb question here. What is your opinion of using graphics and writing code? Any performance comparison? I think with code i can keep the size of app as small as possible. and maybe the memory usage? Thanks in advance.
In some places like table views, doing the drawing in code improves scrolling performance. But in this case it is a matter of preference. The user won’t see a noticeable difference, so do it whichever way is easier for you to maintain in the future.
Instead of adding the notification to the window, what about adding it to your TabBar?
In this way, there is no problem if something is added to the window before your async notification popped up (a modal view for example).
Bonus, if you hide the tabbar, the notification is also hidden.
Don’t you think?
Yep, I like that much better than adding it to the window. I updated the blog post and code on GitHub to add the notification view to the tabbar instead of the window.
[…] How does the Instagram iPhone app implement a custom tab bar notification? […]
Thanks for posting this! I’ll definitely be trying it out in addition to my custom tab bar!
Thanks it’s really help full for me i’ll definitely try to addition.
[…] here […]