1

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.

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.

Community
  • 1
  • 1
Artem Z.
  • 1,243
  • 2
  • 14
  • 36
  • Can you check that put break point in didSelect and touch the first tab then check whether the break point comes first or the selection image appears first? – jailani Jan 24 '14 at 05:42
  • I need to check that the delegate method get called before the selection image appears or after the selection image appears? – jailani Jan 24 '14 at 06:01
  • Can you check that? Please put break point and check it – jailani Jan 24 '14 at 06:19
  • @jai Oh, sorry. If I put the breakpoint before if (tabBarController.selectedIndex==2) (in delegate method) There is no selectionIndicatorImage. Delegate method is the first. If I put the point in the end of the method - there is the same situation. – Artem Z. Jan 24 '14 at 07:54
  • Put logs in your delegate methods and also check that in your project that your are setting selectionIndicatorImage as nill – jailani Jan 28 '14 at 09:38
  • - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { if(tabBarController.tabBar.selectionIndicatorImage == nil) NSLog("Starting point delegate: Selection indicator image is nill"); else NSLog("Ending Point Of delegate: Selection indicator image is available"); – jailani Jan 28 '14 at 09:39
  • if (tabBarController.selectedIndex==2) { [tabBarController.tabBar setSelectionIndicatorImage:nil]; } else { [tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]]; } if(tabBarController.tabBar.selectionIndicatorImage == nil) NSLog("Starting point Of delegate: Selection indicator image is nill"); else NSLog("End point Of delegate: Selection indicator image is available"); } – jailani Jan 28 '14 at 09:40
  • `if(tabBarController.tabBar.selectionIndicatorImage == nil) [tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];` and it works. Add this as an answer and I'll accept it, thanks a lot – Artem Z. Jan 28 '14 at 10:38

1 Answers1

2

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");
   }
Irfan
  • 4,301
  • 6
  • 29
  • 46
jailani
  • 2,260
  • 2
  • 21
  • 45