ios - UITabBar 外观设置 SelectionIndicatorImage 在首次启动 iOS 7 时不起作用

标签 ios uitabbar

我有一个自定义的 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*******");
}

这在 iOS5+ 中一切正常,但在 7 中,首次加载第一个 TabBarItem 时,项目指示器为白色,按钮似乎已被选中,但未加载“selectedTab”图像。

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

customizeTabBar 被调用,但所选图像不会在首次启动时出现。

didEndCustomizingViewControllers 似乎根本没有被调用。

这不适用于 iOS7 上的模拟器或设备 - 但适用于 iOS5、6。

有什么想法吗? 提前致谢。

最佳答案

除了通过外观之外,再次直接为选项卡栏设置选择指示器图像对我有用!

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

关于ios - UITabBar 外观设置 SelectionIndicatorImage 在首次启动 iOS 7 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18820988/

相关文章:

ios - 如何更改 UITabBar 选择颜色

ios - 设计IOS标签栏的自定义图标

ios - 将 TAB BAR 添加到 uitableviewcontroller

ios - 如何知道 UIVIewController 是否显示为 Peek Preview Controller

ios - 可扩展单元格不会扩展

objective-c - NSDictionay 总是返回 nil

ios - 自动向下滚动到下一个 UITextField

ios - Swift 中 NSLayoutConstraints 的奇怪行为

ios - 单击相应按钮后如何转到标签栏 Controller

ios - 标签栏在 iOS 中不可见