iEinstein
iEinstein

Reputation: 2100

UITabBarButton index getting changed while applying selected image

Currently, I am running with the problem while applying the selected image over UITabBarButton. When I am doing this it changes the hierarchy of UITabBarbutton within tabBar.items. Really don't have any idea why this is happening. Posting the code-

 UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
 UITabBarController *tabBarController =(UITabBarController*)[keyWindow rootViewController];
 UITabBar *tabBar = tabBarController.tabBar;
 UITabBarItem *targetTabBarItem = [[tabBar items] objectAtIndex:1]; // whichever tab-item
 [targetTabBarItem setSelectedImage:image];
 [targetTabBarItem setImage:image];

What I am doing here is-

Getting the image from the server and applying it over the particular UItabBarbutton image. Whenever I set it, it takes the UITabbarButton and inserts at the last position of all the UITabBarButtons.

Initial Images

Initial Image

Whenever image got changed from the server it's hierarchy got changed somewhat like this- After Effect Image

enter image description here

Here we can see that initially, the order of buttons was Tab1, Tab2 and Tab3, while after changing the selected image it got changed into Tab1, Tab3 and Tab2. It is indicating that the selected image index pops out and always inserts to last in TabBarButtons collection. If I change the selected image at index 0 initially the order would be Tab2, Tab3 and Tab1.

So anyone here can give a brief idea which is going wrong in the code? Sorry for the long post and thanks in advance as well.

Thanks.

Upvotes: 0

Views: 129

Answers (1)

R4N
R4N

Reputation: 2595

The ordering of the tab bar view hierachy as displayed in the view debugger should be totally independent from the backing tabBarController's tabBar property which is holding the tab bar item array.

My guess is the reason you are seeing this adjustment of the ordering the view hierarchy is because changing the image property will either cause it to be redrawn or removed/readded to the view hierarchy so it will then be the last item added there (this should not at all affect the ordering of the tabBarController.tabBar.items)

You'll see the same thing occur if you just change the title of a tab bar item.

I rigged up a button to change the index 1 tab bar item image similar to the code you show, then display the tab bar ordering in a label:

- (IBAction)changeTabBarImage:(id)sender {
    UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
    UITabBarController *tabBarController =(UITabBarController*)[keyWindow rootViewController];
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *targetTabBarItem = [[tabBar items] objectAtIndex:1]; // whichever tab-item
    NSMutableString *tabBarOrder = [[NSMutableString alloc] init];
    for (NSUInteger tabBarIndex = 0; tabBarIndex < [tabBar items].count; tabBarIndex++) {
        [tabBarOrder appendString:[NSString stringWithFormat:@"index: %lu title: %@\n", tabBarIndex, [[tabBar items] objectAtIndex:tabBarIndex].title]];
    }
    self.firstLabel.text = [NSString stringWithString:tabBarOrder];
    UIImage *image = [UIImage imageNamed:@"sync"];
    [targetTabBarItem setSelectedImage:image];
    [targetTabBarItem setImage:image];
}

The only image ever changed is the tab bar at index 1 and the ordering of the tabBarController.tabBar.items remains the same, even though the view hierarchy ordering in the view debugger changes (as you show in your screenshots).

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaViewsGuide/WorkingWithAViewHierarchy/WorkingWithAViewHierarchy.html

If you're seeing something different, i'd recommend adding some additional code, as I'm not able to reproduce the issue with just what you've aded.

Upvotes: 0

Related Questions