2011-10-17 30 views
13

Tôi có một đoạn mã đơn giản đặt một hình nền trên thanh tabBar.Hình nền UITabBar tùy chỉnh không hoạt động trong iOS 5 trở lên

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

Tính năng này hoạt động tốt trong iOS 4 nhưng khi thử nghiệm trong iOS 5, nó không hoạt động.

Tôi đang cố gắng để làm như sau:

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

Alas, đây không phải đang làm việc ... Có thể bất cứ ai cung cấp một giải pháp?

Trả lời

37

iOS5 cung cấp Proxy UIAppearance.

Ngoài ra, cách tốt nhất để chuyển mã dựa trên khả năng (trong trường hợp này là respondsToSelector) thay vì phiên bản iOS - đó là giả định mong manh (ai nói nó không thay đổi trong tương lai).

Bạn có thể thiết lập nó chỉ dụ đó hoặc trên toàn cầu cho tất cả các thanh tab:

// 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 là đúng. Bạn không nên căn cứ mã của bạn trên một mã phiên bản mà là tìm ra nếu một tính năng tồn tại trong hệ điều hành hiện tại là một cách tốt hơn để tiếp cận vấn đề này. – awDemo

3

Sau khi xem xét các bài báo khác nhau, tôi thấy câu trả lời cho bất cứ ai đó là có cùng một vấn đề:

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

Làm việc như một say mê! Thưởng thức.

+0

Cảm ơn, nó làm việc cho tôi, đã thiết lập chức năng này cho cả iphone 4 và 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

Tôi nhận thấy điều này đã được giải quyết, tôi đăng bài này cho những người khác có cùng vấn đề với tôi. Tôi muốn thêm hình nền vào tab đã chọn trong thanh tabbar. Đây là giải pháp:

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

Dòng thứ hai ở đây thêm ảnh nền vào tab đã chọn trong thanh tab.

+0

Giải pháp đơn giản nhất thường là tốt nhất, hoạt động tuyệt vời !! – Emil

-1

Có một cái gì đó mới trong iOS 5 forBarMetrics:UIBarMetricsDefault

Here is the apple doc on that

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

Các vấn đề liên quan