UITabBar外观setSelectionIndicatorImage在首次启动iOS7时不起作用

cra*_*igk 15 uitabbar ios

我有一个自定义的UITabBar并在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*******");
}
Run Code Online (Sandbox Code Playgroud)

这在iOS5 +中都很好,但在第一次加载第一个TabBarItem时,项目指示器为白色并且按钮似乎已被选中,但未加载"selectedTab"图像.

当我按下另一个选项卡时,新选项卡显示为红色且显示正确 - 与此后选择的第一个或任何选项卡栏项一样 - 它仅在首次启动时不起作用.

调用customizeTabBar但首次启动时不会显示所选图像.

didEndCustomizingViewControllers似乎根本没有被调用.

这在iOS7上的模拟器或设备中不起作用 - 但在iOS5,6上也是如此.

有任何想法吗?提前致谢.

Yul*_*Sh. 13

再次设置标签栏的选择指示图像,除了通过外观做,对我有用!

- (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;
}
Run Code Online (Sandbox Code Playgroud)


cze*_*uff 5

我看到了同样的问题.这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

以下是我设置标签栏的方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

最后,这是标签栏自定义块:

- (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]];
}
Run Code Online (Sandbox Code Playgroud)

如上所述,"选项卡选择"图像未加载到第一个选项卡上.但是,我在[self.window makeKeyAndVisible]之后添加了以下行,以便我的选项卡启动时打开了另一个选项卡,并且"tab-selected"图像确实显示在此选项卡上:

    [tabBarController setSelectedIndex:1];
Run Code Online (Sandbox Code Playgroud)

所以这是我最终确定的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];
        [tabBarController setSelectedIndex:1];
        [tabBarController setSelectedIndex:0];

        return YES;
    }
Run Code Online (Sandbox Code Playgroud)