Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPDATE: As of iOS 7.1 this technique no longer works (if the user taps the same tab twice in succession, the background colour is cleared).</strong></p> <hr> <p><code>UITabBarItem</code> is a subclass of <code>UIBarItem</code>, everything is more painful because UIBarItem doesn't subclass <code>UIView</code>; however, <code>UITabBarItem</code> <em>contains</em> one. What follows manipulates that view, and therefore might be rejected if submitted to the AppStore.</p> <p><strong>1) Subclass UITabBarItem</strong> </p> <p>Create a subclass of UITabBarItem and add a new <code>selected</code> property to its header, like so:</p> <pre><code>@interface ALDTabBarItem : UITabBarItem @property (nonatomic, assign, getter = isSelected) BOOL selected; @end </code></pre> <p>UITabBarItems have a view property, but it isn't exposed. We can extend the class to access it, and then create a custom setter on the <code>selected</code> property to change the background colour, like so:</p> <pre><code>#import "ALDTabBarItem.h" @interface ALDTabBarItem (ALD) @property (nonatomic, strong) UIView *view; @end @implementation ALDTabBarItem - (void)setSelected:(BOOL)selected { if(selected) self.view.backgroundColor = [UIColor redColor]; else self.view.backgroundColor = [UIColor clearColor]; } @end </code></pre> <p><strong>2) Update your UITabBarController delegate</strong> </p> <p>Add the following code to the delegate of your UITabBarController, which sets the selected states of the UITabBar:</p> <pre><code>- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { for(ALDTabBarItem *myItem in tabBar.items) myItem.selected = (myItem == item); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload