Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBar appearance setSelectionIndicatorImage does not work on first launch iOS7

Tags:

ios

uitabbar

I have a customised UITabBar and use the following code in the AppDelegate:

- (void)tabBarController:(MainUITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self customizeTabBar];
}


- (void)customizeTabBar {

    NSLog(@"*******customizeTabBar*******");
    UIImage *tabBackground = [[UIImage imageNamed:@"unselectedtab"]
                  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // Set background for all UITabBars
    [[UITabBar appearance] setBackgroundImage:tabBackground];
    // Set tint color for the images for all tabbars
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
    // Set selectionIndicatorImage for all tabbars
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selectedtab"]];

} 

- (void)tabBarController:(MainUITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    NSLog(@"*******didEndCustomizingViewControllers*******");
}

This is all fine in iOS5+ but in 7 on first load the first TabBarItem the item indicator is white and the button seems to have been selected but the "selectedTab" image is not loaded.

When I press another tab the new tab is red and appears correctly - as does the first or any tab bar item selected after this - it only doesn't work on first launch.

customizeTabBar get called but the selected image does not appear on first launch.

didEndCustomizingViewControllers does not seem to get called at all.

This doesn't work in emulator or device on iOS7 - but does on iOS5, 6.

Any ideas? Thanks in advance.

like image 904
craigk Avatar asked Sep 16 '13 05:09

craigk


3 Answers

Setting the selection indicator image for the tab bar directly once again, apart from doing it via appearance, worked for me!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ....

    UITabBarController *tabBarContr = (UITabBarController *)self.window.rootViewController;
    ...
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];

    // iOS7 hack: to make selectionIndicatorImage appear on the selected tab on the first app run
    [[tabBarContr tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];

    return YES;
}
like image 100
YuliaSh. Avatar answered Nov 18 '22 20:11

YuliaSh.


I am seeing this exact same issue. Here is my didFinishLaunching

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self applyStyleSheet];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.backgroundColor = [UIColor redColor];
    self.window.tintColor = [UIColor whiteColor];
    UITabBarController *tabBarController = [self setupTabBarController];
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

Here is how I setup the tab bar:

- (UITabBarController *)setupTabBarController
{
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:[[SecondViewController alloc] init]];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:[[ThirdViewController alloc] init]];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:[[FourthViewController alloc] init]];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:[[FifthViewController alloc] init]];
    [tabBarController setViewControllers:@[nav1, nav2, nav3, nav4, nav5]];

    return tabBarController;
}

And finally, this is the tab bar customization block:

- (void)applyStyleSheet
{
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
    [tabBar setTintColor:[UIColor whiteColor]];
    [tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"tab-selected"]];
    [tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}

As stated, the "tab-selected" image is not loaded on the first tab. However, I added the following line after [self.window makeKeyAndVisible] so that my tab starts up with a different tab opened, and the "tab-selected" image does show up on this tab:

    [tabBarController setSelectedIndex:1];

So here's my finalized didFinishLaunching with the subtle hack that makes it work :)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self applyStyleSheet];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.backgroundColor = [UIColor redColor];
        self.window.tintColor = [UIColor whiteColor];
        UITabBarController *tabBarController = [self setupTabBarController];
        self.window.rootViewController = tabBarController;
        [self.window makeKeyAndVisible];
        [tabBarController setSelectedIndex:1];
        [tabBarController setSelectedIndex:0];

        return YES;
    }
like image 23
czeluff Avatar answered Nov 18 '22 20:11

czeluff


ok.

not the best of fixes but hey have to submit.

Remove the customisation code in the appdelegate and in the projects xib file (is an old project) on the TabBars attributes inspector (using xcode 5) - add the tab bars background and selection images.

This works for ios7 without the need for any of the customisation code in the appdelegate.

For pre iOS5 + 6 (this app only supports 5+) however we still need the code so I added a simple check for version and kept the code as is:

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

if(SYSTEM_VERSION_LESS_THAN(@"7.0"))

    {

        UIImage *tabBackground = [[UIImage imageNamed:@"unselectedtab"]

                                  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

        // Set background for all UITabBars

        [[UITabBar appearance] setBackgroundImage:tabBackground];
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

    // Set tint colour for the images for all tabbars

    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];

    // Set selectionIndicatorImage for all tabbars

    [[UITabBar appearance] setSelectionIndicatorImage:nil];

    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selectedtab.png"]];

}
like image 2
craigk Avatar answered Nov 18 '22 22:11

craigk