Custom UITabBar background image not working in iOS 5 and later

后端 未结 5 2160
不思量自难忘°
不思量自难忘° 2020-12-01 11:13

I have a simple piece of code that places a background image on the tabBar.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@         


        
相关标签:
5条回答
  • 2020-12-01 11:53
    //---- 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];
    
    }
    
    0 讨论(0)
  • 2020-12-01 11:53

    I realize this has been solved, I'm posting this for others who had the same issue as me. I wanted to add a background image to the selected tab in a tabbar. Here is the solution:

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

    The second line here adds a background image to the selected tab in a tabbar.

    0 讨论(0)
  • 2020-12-01 11:56

    After reviewing various articles, I found the answer for anyone that's having the same problem:

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

    Works like a charm! Enjoy.

    0 讨论(0)
  • 2020-12-01 12:03

    There is something new in iOS 5 forBarMetrics:UIBarMetricsDefault

    Here is the apple doc on that

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

    0 讨论(0)
  • 2020-12-01 12:07

    iOS5 offers the UIAppearance Proxy.

    Also, it's best practice to switch your code based on the capability (in this case it's respondsToSelector) instead of iOS version - that's a fragile assumption (who's to say it doesn't change in the future).

    You can set it for just that instance or globally for all tab bars:

    // 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
    }
    
    0 讨论(0)
提交回复
热议问题