2011-10-17 71 views
13

我有一段简单的代码将背景图像放在tabBar上。自定义UITabBar背景图像不适用于iOS 5及更高版本

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

唉,这是不工作...任何人都可以提供一个解决方案?

回答

37

iOS5提供了UIAppearance Proxy。

此外,最好的做法是根据能力切换代码(在本例中为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 
} 
+2

bryanmac是正确的。您不应该将代码基于版本代码,而应该查明当前操作系统中是否存在功能是解决此问题的更好方法。 – awDemo

3

审查各种文章后,我找到了答案这是使用了同样的问题的人:

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

奇迹般有效!请享用。

+0

谢谢,它为我的工作,为iPhone 4和iPhone5都设置了此功能。 – ravinder521986

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

} 
3

我意识到这个问题已经解决了,我把这张贴发给其他与我有同样问题的人。我想将一个背景图像添加到选项卡中的选定选项卡。这里是解决方案:

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

第二行在这里添加一个背景图片到tabbar中的选定选项卡。

+0

最简单的解决方案往往是最好的,很棒! – Emil

-1

也有一些是新的iOS 5中 forBarMetrics:UIBarMetricsDefault

Here is the apple doc on that

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

相关问题