UITabBar外观集SelectionIndicatorImage在首次启动iOS 7时不起作用

时间:2013-09-16 05:35:13

标签: 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 +中都很好但是在第一次加载第一个TabBarItem时,项目指示器为白色并且按钮似乎已被选中,但未加载“selectedTab”图像。

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

调用

customizeTabBar,但首次启动时不会显示所选图像。

didEndCustomizingViewControllers似乎根本没有被调用。

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

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

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
}

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

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

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

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

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

    [tabBarController setSelectedIndex:1];

所以这是我最终确定的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;
    }

答案 2 :(得分:2)

确定。

不是最好的修复,但嘿必须提交。

在TabBar属性检查器上的appdelegate和项目xib文件(是一个旧项目)中删除自定义代码(使用xcode 5) - 添加标签栏背景和选择图像。

这适用于ios7,无需appdelegate中的任何自定义代码。

对于iOS5 + 6之前的版本(这个应用程序只支持5+)但是我们仍然需要代码,所以我添加了一个简单的版本检查并按原样保存代码:

#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"]];

}

答案 3 :(得分:1)

我认为在iOS 7中为新应用程序设计时我也遇到了同样的问题!! iOS 7已经构建了更多不同的东西,因为我们都习惯了不同的东西。

据我所知,我们都在使用StoryBoards,并且无法在我们的代码中集成Segues! :) 所以我选择不要乱用代码,之后我尝试了大部分关于此的StackOverFlow Answers! :)因为,你为什么要这样做,当你给了Goody Good 界面生成器(IB)故事登机工具

问题:
当我们设置了我们的选项标签图像,专门用于标签栏的背景图像时,它没有显示我们在代码中设置的图像选择了哪个标签...... ???

解决方案
以下是我为解决这个问题而做的StoryBoard设置的截图!

从您的文档大纲面板中选择您的TabBarController:
Select your TabBarController from your via document outline panel

从“工具”面板设置标签栏的设置:
Set your settings for the Tab Bar from the Utilities Panel

然后您的程序已设置为运行!它现在知道当应用程序首次显示第一个选项卡视图时选择第一个选项卡,并且当选择每个选项卡时,应该为所有选项卡栏指示器显示哪个图像! :)
希望你们都有线索! 如果我帮助你我很开心! 但如果我浪费你的时间,我很抱歉! :( 但请相信我,这让我感觉像是一个魅力!

答案 4 :(得分:0)

- (void)customizeTabBar {

 UIImageView *customizeTabBar = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
customizeTabBar.image=[UIImage imageNamed:@"Tab_bar.png"];

firstTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab1.png"] highlightedImage:[UIImage imageNamed:@"tab11.png"]];
[firstTab setFrame:CGRectMake(8.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: firstTab];

secondTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab2"] highlightedImage:[UIImage imageNamed:@"tab22"]];
[secondTab setFrame:CGRectMake(115.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: secondTab];

thirdTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab3"] highlightedImage:[UIImage imageNamed:@"tab33"]];
[thirdTab setFrame:CGRectMake(223.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: thirdTab];
self.tabBar.tag=10;
[self.tabBar addSubview:customizeTabBar];

}