Automatically highlight UITabBar Button
I'm using this method http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited to create a custom UITabBar and loading a particular view when a certain TabBarItem is clicked.
The problem is that init开发者_开发知识库ially the first view is loaded, but the first tab bar item is not highlighted. Is there a way to force the highlight? I'm not using the tabbarcontroller so I can't use its methods.
If you make an instance variable: UITabBar *tabBar;
a property:
@property (nonatomic, assign) IBOutlet UITabBar *tabBar;
and connect this property to the UITabBar in Interface Builder, you can use:
for(UITabBarItem *tab in tabBar.items) {
if ([tab.title isEqualToString: @"My Tab Title"]) {
tabBar.selectedItem = tab;
}
}
This works if all tabs have a unique title, which is normally the case.
精彩评论