2011-10-17 72 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];

相關問題