UITabBar外观设置SelectionIndicatorImage在首次启动iOS7时不工作

UITabBar外观设置SelectionIndicatorImage在首次启动iOS7时不工作,ios,uitabbar,Ios,Uitabbar,我有一个定制的UITabBar,并在AppDelegate中使用以下代码: - (void)tabBarController:(MainUITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { [self customizeTabBar]; } - (void)customizeTabBar { NSLog(@"*******customize

我有一个定制的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中,第一次加载第一个选项卡时,项目指示器为白色,按钮似乎已被选中,但未加载“selectedTab”图像

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

调用customizeTabBar,但第一次启动时未显示所选图像

DiEndCustomingViewController似乎根本没有被调用

这在iOS7上的模拟器或设备中不起作用,但在iOS5、6上起作用

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

好的

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

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

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

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

}

我看到了完全相同的问题。这是我的生日礼物

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

    [tabBarController setSelectedIndex:1];
下面是我最终的didFinishLaunching,它采用了一种微妙的技巧,使它能够工作:)


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

- (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 7中的新应用程序设计时也遇到了同样的问题!! iOS 7构建了更多不同的东西,因为我们都习惯了不同的东西

据我所知,在这里,我们都在使用故事板,无法将这些片段集成到我们的代码中!:) 因此,在我尝试了与此相关的大多数StackOverFlow答案之后,我选择了不要弄乱代码()因为,当您提供了一个好的界面生成器(IB)故事板工具时,为什么要这样做

问题:
当我们设置了我们选择的标签图像,特别是标签栏的背景图像时,它不会显示哪个标签与我们在代码中设置的图像一起被选中

解决方案
下面是我为解决这个问题所做的故事板设置的截图

从via文档大纲面板中选择您的Tabbar控制器:

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

然后,您的程序设置为运行!现在它知道,当应用程序第一次显示第一个选项卡视图时,第一个选项卡已被选中,并且在选择每个选项卡时,所有选项卡栏指示器应显示哪个图像!:)
希望你们都有线索!!! 如果我帮了你,我很高兴!!! 但是如果我浪费了你的时间,我很抱歉!!!:(
但是相信我,这对我来说很有吸引力!!!

你的代码不会有什么不同。问题在于版本。这不是版本问题。代码在IOS7中也能正常工作。我只想更改选定的指示器图像,而不是图标,代码在启动后都能正常工作(当我按下另一个选项卡并返回时),仅在初始视图加载上要进一步记录这一点,之所以有效,是因为ios7+要求您直接手动设置选项卡栏实例的selectionIndicatorImage,而不是通过外观协议。因此,如果您想在代码中像我一样设置内容,而不是在xib中,您仍然可以不使用外观。我可以确认这个答案对我也很有效,在我看来,这是我认为最直接的解决方案。为了正确的答案,改成这个答案-比下面我的具体案例更有效-谢谢@YuliaShThis对我不起作用,但在我放置“iOS7 hack”时效果很好在第一个选项卡栏的视图控制器的viewDidLoad中。这是iOS 7.0中的一个bug,更新您的设备它应该会自行修复:)很好,您已经展示了IB解决方案,但建议每个人都使用IB有点太多了;不要泛泛。很多人(像我和我工作的几家公司)觉得这很糟糕。正常合并冲突、继承?为什么开发者需要这样的东西?(故事板不提供继承,使得XIBs中的所有东西都很弱)。@Vive Wow!我尊重你的观点!谢谢,这当然也拓宽了我的视野!:-)没错,我不应该建议使用IB,也不应该推广它!然而,我想这对那些处于初级阶段或在同一点上挣扎的人是有帮助的!
- (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;
}