2011-10-17 8 views
13

Mam prosty fragment kodu, który umieszcza obraz tła na pasku tab.Niestandardowy obraz tła UITabBar nie działa w systemie iOS 5 i nowszym

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

Działa to dobrze w systemie iOS 4, ale podczas testowania w systemie iOS 5 nie działa.

Próbuję wykonać następujące czynności:

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

niestety, to nie działa ... Czy ktoś może zaoferować rozwiązanie?

Odpowiedz

37

iOS5 oferuje serwer proxy UIAppearance.

Najlepiej przećwiczyć kod na podstawie możliwości (w tym przypadku jest to responsStoSector) zamiast wersji iOS - to delikatne założenie (kto by powiedział, że nie zmieni się w przyszłości).

Można ustawić go tylko dla tej instancji lub globalnie dla wszystkich barów zakładka:

// 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 jest poprawna. Nie powinieneś opierać swojego kodu na kodzie wersji, ale raczej dowiedzieć się, czy funkcja istniejąca w systemie operacyjnym jest lepszym sposobem podejścia do tego problemu. – awDemo

3

Po przejrzeniu różnych artykułów, znalazłem odpowiedź dla każdego, kto jest ten sam 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]; 

Działa jak marzenie! Cieszyć się.

+0

Dzięki, działa dla mnie, ustawiłem tę funkcję zarówno dla iPhone 4 i 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

Rozumiem to zostało rozwiązane, jestem delegowania to dla innych, którzy mieli ten sam problem jak ja. Chciałem dodać obraz tła do wybranej karty na pasku zakładek. Oto rozwiązanie:

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

Drugi wiersz tutaj dodaje obraz tła do wybranej karty w pasku zakładek.

+0

Najprostsze rozwiązanie jest często najlepsze, działa świetnie !! – Emil

-1

Jest coś nowego w iOS 5 forBarMetrics:UIBarMetricsDefault

Here is the apple doc on that

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

Powiązane problemy