【问题标题】:UITabBar appearance setSelectionIndicatorImage does not work on first launch iOS7UITabBar 外观 setSelectionIndicatorImage 在首次启动 iOS7 时不起作用
【发布时间】:2013-09-16 05:35:13
【问题描述】:

我有一个自定义的 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 上有效。

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

【问题讨论】:

    标签: ios uitabbar


    【解决方案1】:

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

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

    【讨论】:

    • 我可以确认这个答案也对我有用,我认为这是我认为最直接的解决方案..
    • 改成这个以获得正确答案 - 比我下面的具体情况更适用 - 谢谢@YuliaSh
    • 这对我不起作用,但是当我将“iOS7 hack”放在第一个标签栏的视图控制器的 viewDidLoad 中时效果很好。
    • 它是 iOS 7.0 中的一个错误,请更新您的设备,它应该会自行修复 :)
    【解决方案2】:

    我看到了同样的问题。这是我的 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] 之后添加了以下行,以便我的选项卡启动时打开另一个选项卡,并且“选项卡选择”图像确实显示在此选项卡上:

        [tabBarController setSelectedIndex:1];
    

    所以这是我最终确定的 didFinishLaunching 以及使它工作的微妙 hack :)

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

    【讨论】:

      【解决方案3】:

      好的。

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

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

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

      对于pre 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"]];
      
      }
      

      【讨论】:

      • 为了进一步记录这一点,这样做的原因是因为 ios7+ 要求您直接手动设置选项卡栏实例的 selectionIndicatorImage,而不是通过外观协议。所以如果你想像我一样用代码而不是 xib 来设置你的东西,你仍然可以不使用它的外观。
      【解决方案4】:

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

      据我了解,我们都在使用 StoryBoards,并且无法将 Segues 集成到我们的代码中! :) 所以我选择不要弄乱代码,在我尝试了大部分关于此的 StackOverFlow 答案之后! :) 因为,当您提供 Goody Good Interface Builder (IB)Story Boarding Tool 时,您为什么要这样做?

      问题:
      当我们设置了我们选择的标签图片,标签栏的背景图片时,它不会显示我们在代码中设置的图片选择了哪个标签...???

      解决方案
      以下是我为解决此问题所做的 StoryBoard 设置的屏幕截图!

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

      从实用程序面板设置标签栏的设置:

      那么您的程序就可以运行了!它现在知道当应用程序第一次显示第一个选项卡视图时选择了第一个选项卡,并且当每个选项卡栏指示器都被选中时,应该为所有选项卡栏指示器显示哪个图像! :)
      希望大家有所了解!!! 如果我帮助了你,我很高兴!!! 但是如果我浪费了你的时间我很抱歉!!! :( 但相信我,这对我很有帮助!!!

      【讨论】:

      • 你展示了 IB 解决方案很好,但是建议每个人都在使用 IB 有点过分;P。不要一概而论。很多人(比如我和少数与我合作的公司)觉得这很糟糕。正常的合并冲突,继承?为什么开发人员需要这样的东西? (故事板不提供继承,使得 XIB 中的所有内容也很弱)。
      • @Vive 哇!我尊重你的观点!谢谢,它当然也拓宽了我的视野! :-) 没错,我不应该建议使用 IB,也不应该泛化它!但是,我想这对处于初学者阶段或在同一点上苦苦挣扎的人有帮助!
      【解决方案5】:
      - (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];
      
      }
      

      【讨论】:

      • 您的代码不会有任何区别。问题属于版本。
      • 这不是版本问题。代码在 IOS7 中也能完美运行。
      • 我只想更改选定的指示器图像而不是图标,代码在启动后一切正常(当我按下另一个选项卡并返回时),只是在初始视图加载时
      猜你喜欢
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 2011-02-20
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多