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

我有一个自定义的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时,项目指示器是白色的,并且该button似乎已被选中,但“selectedTab”图像未加载。

当我按下另一个标签页时,新的标签页是红色的,并且正确显示 – 第一个或之后select的任何标签栏项也是如此 – 它仅在首次启动时不起作用。

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

didEndCustomizingViewControllers似乎并没有被调用。

这在iOS7的仿真器或设备上不起作用 – 但是在iOS5,6上。

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

直接再次为选项卡设置select指示图像,除了通过外观 ,为我工作!

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

我看到这个完全相同的问题。 这是我的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]]; } 

如上所述,“制表符select”图像不会加载到第一个选项卡上。 不过,我在[self.window makeKeyAndVisible]之后添加了以下行,以便我的选项卡以打开的不同选项卡启动,并在此选项卡显示“选定选项”图像:

  [tabBarController setSelectedIndex:1]; 

所以这里是我最后确定的didFinishLaching与微妙的黑客,使其工作:)

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

好。

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

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

这适用于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"]]; } 

我觉得我也有同样的问题,当我的devise在iOS 7的新应用程序! 由于我们都习惯于不同的东西,所以iOS 7已经被构build了更多不同的东西。

在这里,据我所知,我们都在使用StoryBoards,并且无法将这些Segues集成到我们的代码中! :)所以我select不要惹的代码 ,我尝试了大部分关于这个的StackOverFlow答案! :)因为,为什么你想这样做,当你已经给了一个Goody良好的界面生成器(IB)故事登机工具

题:
当我们已经设置了我们所选的标签图像,专门为标签栏的背景图像,它不显示哪个选项卡与我们在代码中设置的图像select… ???


以下是我解决这个问题的StoryBoard设置的屏幕截图!

从您的文档大纲面板中select您的TabBarController:
从您的文档大纲面板中选择您的TabBarController

从“实用程序”面板设置选项卡栏的设置:
从“实用程序”面板中设置选项卡栏的设置

然后你的程序设置运行! 现在它知道,当应用程序第一次显示第一个标签视图时,第一个标签被选中,当select每个标签栏指示符时,应该显示哪个图像应该显示哪个图像! 🙂
希望你们有一个线索! 如果我帮你,我很开心! 但是如果我浪费了你的时间,我真的很抱歉! :(但相信我,这使我像一个魅力!

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