【问题标题】:Custom UITabBar background image not working in iOS 5 and later自定义 UITabBar 背景图像在 iOS 5 及更高版本中不起作用
【发布时间】:2011-12-09 16:07:05
【问题描述】:

我有一段简单的代码可以在 tabBar 上放置一个背景图像。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];

这在 iOS 4 中运行良好,但在 iOS 5 中测试时,它不起作用。

我正在尝试执行以下操作:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];

NSString *reqSysVer = @"4.3";
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];

if ([iOSVersion compare:reqSysVer options:NSNumericSearch] !=NSOrderedDescending) {
    // code for iOS 4.3 or below
    [self.tabBarController.tabBar insertSubView:imageView atIndex:0];
}
else {
    // code for iOS 5
    [self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}

[imageView release];

唉,这不起作用...谁能提供解决方案?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch ipad


    【解决方案1】:

    iOS5 提供 UIAppearance 代理。

    此外,最佳做法是根据功能(在本例中为 respondsToSelector)而不是 iOS 版本来切换代码 - 这是一个脆弱的假设(谁会说它在未来不会改变)。

    您可以仅为该实例设置它,也可以为所有标签栏全局设置它:

    // not supported on iOS4    
    UITabBar *tabBar = [tabController tabBar];
    if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
    {
        // set it just for this instance
        [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]];
    
        // set for all
        // [[UITabBar appearance] setBackgroundImage: ...
    }
    else
    {
        // ios 4 code here
    }
    

    【讨论】:

    • bryanmac 是正确的。您的代码不应基于版本代码,而是找出当前操作系统中是否存在某个功能是解决此问题的更好方法。
    【解决方案2】:

    在查看了各种文章后,我为遇到相同问题的任何人找到了答案:

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
        //iOS 5
        [self.tabBarController.tabBar insertSubview:imageView atIndex:1];
    }
    else {
        //iOS 4.whatever and below
        [self.tabBarController.tabBar insertSubview:imageView atIndex:0];
    }
    
    [imageView release];
    

    像魅力一样工作!享受吧。

    【讨论】:

    • 谢谢,它为我工作,已经为 iphone 4 和 iPhone5 设置了这个功能。
    【解决方案3】:
    //---- For providing background image to tabbar
    
    UITabBar *tabBar = [tabBarController tabBar];
    if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
    {
        // ios 5 code here
        [tabBar setBackgroundImage:[UIImage imageNamed:@"PB_MD_footer_navBg_v2.png"]];
    
    }
    else
    {
        // ios 4 code here
    
        CGRect frame = CGRectMake(0, 0, 480, 49);
        UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
        UIImage *tabbag_image = [UIImage imageNamed:@"PB_MD_footer_navBg_v2.png"];
        UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
        tabbg_view.backgroundColor = tabbg_color;
        [tabBar insertSubview:tabbg_view atIndex:0];
    
    }
    

    【讨论】:

      【解决方案4】:

      我知道这个问题已经解决了,我把这个发布给和我有同样问题的其他人。我想将背景图像添加到选项卡栏中的选定选项卡。这是解决方案:

      [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar.png"]];
      [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar-item.png"]];
      

      此处的第二行将背景图像添加到选项卡栏中的选定选项卡。

      【讨论】:

        【解决方案5】:

        iOS 5 中有一些新功能 forBarMetrics:UIBarMetricsDefault

        Here is the apple doc on that

        [[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-02
          • 1970-01-01
          • 1970-01-01
          • 2016-03-13
          • 1970-01-01
          • 2023-03-13
          • 2021-09-08
          • 1970-01-01
          相关资源
          最近更新 更多