February 23, 2009

Tab Bar Icons (Part 1)

Tab bars are for toggling between application views. This is a common controller on the screen-constrained iPhone. Not to be confused with toolbars, tab bars are the black bars at the bottom of the screen with gray unselected icons and one blue selected icon. You see tab bars in the phone and ipod and YouTube apps, among others. Here is an example, from the ipod app:
When you are creating a tab bar for your own application, you face the problem of providing the icons. You have several options here. Your first option is to use one of Apple's system icons. Like the navigation bar, you have access to some of Apple's system icons, and they even encourage you to use them. Unfortunately, there are a couple caveats with using these. First, the list is pretty skimpy. Second, the icons include the text as well. You cannot set the image and the text separately; it's both or nothing. Here is what is offered:
icon text enum
More UITabBarSystemItemMore
Favorites UITabBarSystemItemFavorites
Featured UITabBarSystemItemFeatured
Top 25 UITabBarSystemItemTopRated
Recent UITabBarSystemItemRecents
Contacts UITabBarSystemItemContacts
History UITabBarSystemItemHistory
Bookmarks UITabBarSystemItemBookmarks
Search UITabBarSystemItemSearch
Updates UITabBarSystemItemDownloads
Most Recent UITabBarSystemItemMostRecent
Most Viewed UITabBarSystemItemMostViewed
If you can make use of them, you access them via an enum for each defined in the UITabBarItem class. Here is how you would programatically set your tab bar image to the magnifying glass:
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
You execute this in your UIViewController subclass that you are putting into the tab bar. This is best done in your view controller's initializer method:
- (id) init
{
self.title = "search";
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
return self;
}  
If the system icons don't meet your needs, some people have suggested doing a screen capture of an app that has an icon you like. Ignoring the legalities of this, this may not give you good results because Cocoa-Touch has already applied affects to the icons. Your next option would be to search for some free or pay-for icons. I noticed many freelance graphics designers putting their shingle up and offering their services. I didn't look long, but I did not find any suitable, free icons for iPhone tab bars. This leaves you one last option: create your own. So the first questions that come up are:
  1. what format?
  2. what size?
  3. what colors?
After researching these questions, I found some conflicting answers and lots of confusion among the iPhone developer community. I will try to summarize and disperse the clouds. For format, this is pretty well agreed upon to be PNG, which is the image format of choice on the iPhone. You're not limited to this format, but PNG is the recommended format and gives best performance. The background should be transparent, else your icon will be a plain square. The size issue is a bit more contentious. I saw reports of sizes ranging from 20 x 20 all the way up to 57 x 57. Most people don't differentiate between pixel or point sizes, and they are different. Here is what Apple says in the reference documentation for the UITabBarItem:
If this image is too large to fit on the tab bar, it is scaled to fit. The size of an [sic] tab bar image is typically 30 x 30 points.
Note it doesn't say what the max size is before it gets scaled. One poster thought the max size was around 48 x 32 before scaling happens. But if Apple doesn't publish this information explicitly, I would hesitate to use it. You could explore yourself to find out what the max is, but obviously Apple would like to retain some latitude in this area to possibly change it in future firmware releases. I believe 30 x 30 is a safe size that will give you the best look. There also seems to be a great deal of confusion regarding colors. From my research, colors are basically ignored. Here is what the Apple document "View Controller Programming" has to say about it:
The unselected and selected images displayed by the tab bar are derived from the images that you set. The alpha values in the source image are used to create the other images—opaque values are ignored.
In other words, Cocoa Touch will create new images from your source images, using alpha values only. Alpha, in the PNG world, is the degree of a pixel's opacity. Maximum alpha values denote a completely opaque pixels. In the example below this quoted text, they have an example source image and the resulting unselected and selected icons. Color is irrelevant. In part 2 of this article, I will discuss various tools used to compose icons, specifically how to create tab bar icons.

10 comments:

  1. THanks for the post.

    ReplyDelete
  2. Useful, thanks. =)

    ReplyDelete
  3. 30x30 is definitely smaller than the default icon size, at least when there are only four items. I used the 48x32 suggestion that you mentioned, and that seemed to be perfect.

    Thanks for this great post!

    ReplyDelete
  4. dude you are god! :) keep the hard work!

    ReplyDelete
  5. I found some good icons to use on http://glyphish.com/!!!
    We use them in PerTrainer iPhone application http://pertrainer.com

    You can see screenshots here:
    http://pertrainer.com/Home/InfoForIPhoneUsers



    I also found some good icons here:
    http://www.app-bits.com/downloads/iphone-toolbar-icon-set.html
    Happy developing.

    ReplyDelete
  6. Also check out this icon set: 270 Toolbar Icons for iPhone 4 @2x Retina Display / 30 are free, 270 are $20.

    http://www.billybarker.net/iphone-4-retina-icons-for-app-developers/

    ReplyDelete
  7. Worked like a charm :) Nice going

    ReplyDelete
  8. Here is a lot of tab bar icons http://www.aha-soft.com/iconsets.htm#ios

    ReplyDelete
  9. Thanks.

    And look for free mobile icons :
    http://www.pixelledesigns.com/free-mobile-app-icons.htm

    ReplyDelete
  10. http://www.iconbeast.com (One of the best iOS tab bar and toolbar icons out there)

    ReplyDelete