UITabBar appearance setSelectionIndicatorImage does not work on first launch iOS7

后端 未结 5 2273
你的背包
你的背包 2021-02-13 05:51

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

- (void)tabBarController:(MainUITabBarController *)tabBarController didSelectViewCont         


        
5条回答
  •  一生所求
    2021-02-13 06:16

    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;
        }
    

提交回复
热议问题