Today I pushed a nice little update to the Tabbar Support branch of my iOS-Universal App Template project on Github. I added in the "Twitter tabbar slider" indicator. Though it isn't that flashy it was the attention to detail that made Tweetie so popular and usable when it first came. While it has since been duplicated I found it interesting that most examples don't have support for landscape orientation nor for iPads. After looking through @boctor's code on idevrecipes.com I figured I would be able to drop the code in for my template within a few hours.
If it seems too good to be true then it usually is. I realized why I didn't see a whole lot of support for the indicators in other orientations or for the iPad. The first obstacle was orientation support. In @boctor's code the indicator is added as a subview to the main window. This is not a bad practice because it ensures that the indicator is always on top of any subviews within the app. Unfortunately UIWindow doesn't support orientation, so when the device rotates the image looks crooked. The solution is to add the indicator to the tabBarController's view. Now that the image is repositioning itself with the orientation, unfortunately the new position is not centered. To ensure that you will position the indicator in the center of each tabbar item ,regardless of orientation, you have to use the following calculation:
The last piece to the puzzle was to be able to force the indicator to recalculate it's position based upon the orientation. This wouldn't be an issue with UIViewController, but I don't want to add in boilerplate code for each VC that I have. (I already have boilerplate that I am going to abstract out with the next update). Using NSNotification or a custom protocol seemed like overkill…and it was. I ended up creating a category for the UITabBarController that overrides:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
and calls the delegate method for the tabbar that is selected.
The rotation animation on the splitviewcontroller seems a little sluggish so I need to track that down and I am hoping to take out the dependency for an image and draw it programmatically.
A BIG SHOUT OUT to Peter Boctor (http://idevrecipes.com/) and Loren Brichter for being innovative and providing the code that was the basis for my little addition.