自定义UITabBar背景图在iOS 5及更高版本中无法工作

13

我有一段简单的代码,可以在选项卡上放置一个背景图像。

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

哎呀,这个方法不起作用了...有人能提供解决方案吗?

5个回答

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

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

在查看了各种文章后,我找到了解决方案,适用于遇到同样问题的人:

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

像魔法一样好用!享受吧。

谢谢,我的也可以用了,我已经为iPhone 4和iPhone 5设置了这个函数。 - ravinder521986

3

我知道这个问题已经得到解决,但我想为那些和我有同样问题的人提供帮助。我想在选定的标签页中添加背景图像。以下是解决方案:

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

这里的第二行代码将一个背景图片添加到选定选项卡的选项卡栏中。

-1

iOS 5 中有一些新的东西 forBarMetrics:UIBarMetricsDefault

这是苹果文档上的说明

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接