簡體   English   中英

自定義UITabBar背景圖像在iOS 5及更高版本中不起作用

[英]Custom UITabBar background image not working in iOS 5 and later

我有一段簡單的代碼將背景圖像放置在tabBar上。

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

las,這行不通...任何人都可以提供解決方案?

iOS5提供了UIAppearance代理。

另外,最佳實踐是基於功能(在本例中為responsToSelector)而不是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
}
//---- 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];

}

我知道此問題已解決,因此我將其發布給與我有相同問題的其他人。 我想將背景圖像添加到標簽欄中的所選標簽中。 解決方法如下:

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

這里的第二行將背景圖像添加到標簽欄中的所選標簽中。

閱讀各種文章后,我找到了遇到相同問題的任何人的答案:

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

奇跡般有效! 請享用。

iOS 5中有forBarMetrics:UIBarMetricsDefaultforBarMetrics:UIBarMetricsDefault

這是蘋果文件

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM