Reputation: 1243
I'm using the custom UITabBarItem
image for the middle item, so I have to make an UIImage
for selectionIndicatorImage.
According to this answer Same question as mine I've made the code.
UIStoryboard *iPhoneSB = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UITabBarController *tbc = [iPhoneSB instantiateInitialViewController];
self.window.rootViewController = tbc;
tbc.delegate = self;
tbc.selectedIndex = 2;
UITabBar *tb = tbc.tabBar;
NSArray *items = tb.items;
for (UITabBarItem *tbi in items) {
UIImage *image = tbi.image;
tbi.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[tbc.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];
And the delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex==2) {
[tabBarController.tabBar setSelectionIndicatorImage:nil];
} else {
[tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];
}
}
It works, thanks to the answer. But there is some issue. selectionIndicatorImage
is still nil after the first touch inside any UITabBarItem
(sure except the middle).
For example:
On the app launch third UITabBar is selected. Touch at first item - selectionIndicatorImage works good (first item became selected). Touch at third item (particular item) - there is no selectionIndicator (it's good). But after that if I touch for example first - there is no selectionIndicator too. It appears if I touch second after that. Where I was wrong? Thanks in advance.
Upvotes: 1
Views: 765
Reputation: 2270
Put logs in your delegate methods and also check that in your project, your are setting selectionIndicatorImage as nil.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if(tabBarController.tabBar.selectionIndicatorImage == nil)
NSLog("Starting point delegate: Selection indicator image is nill");
else
NSLog("Starting Point Of delegate: Selection indicator image is available");
if (tabBarController.selectedIndex==2) {
[tabBarController.tabBar setSelectionIndicatorImage:nil];
} else {
[tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];
}
if(tabBarController.tabBar.selectionIndicatorImage == nil)
NSLog("Ending point delegate: Selection indicator image is nill");
else
NSLog("Ending Point Of delegate: Selection indicator image is available");
}
Upvotes: 2