自定义UITabBar背景图片在iOS 5及更高版本中不起作用 [英] Custom UITabBar background image not working in iOS 5 and later

查看:240
本文介绍了自定义UITabBar背景图片在iOS 5及更高版本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码片段,在tabBar上放置一个背景图片。

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

这在iOS 4中工作正常,但是在iOS 5中测试时,它不工作。 p>

我试图做以下操作:

  UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@tabBG.png]]; 

NSString * reqSysVer = @4.3;
NSString * iOSVersion = [[UIDevice currentDevice] systemVersion];

if([iOSVersion比较:reqSysVer选项:NSNumericSearch]!= NSOrderedDescending){
// iOS 4.3或更低版本的代码
[self.tabBarController.tabBar insertSubView:imageView atIndex :0];
}
else {
// iOS 5的代码
[self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}

[imageView版本];

唉,这不行...任何人都可以提供解决方案吗?

解决方案

iOS5提供UIAppearance代理。



此外,最好的做法是切换代码基于能力(在这种情况下是respondtoSelector),而不是iOS版本 - 这是一个脆弱的假设(谁说,它不会改变在未来)。



可以为所有标签栏设置它:

  // iOS4不支持
UITabBar * tabBar = [tabController tabBar];
if([tabBar respondingToSelector:@selector(setBackgroundImage :)])
{
//仅为此实例设置
[tabBar setBackgroundImage:[UIImage imageNamed:@tabbar_brn .jpg]];

//为所有设置
// [[UITabBar外观] setBackgroundImage:...
}
else
{
// ios 4 code here
}


I have a simple piece of code that places a background image on the tabBar.

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

This works fine in iOS 4 but when testing in iOS 5, it doesn't work.

I'm trying to do the following:

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, this isn't working... Can anyone offer a solution?

解决方案

iOS5 offers the UIAppearance Proxy.

Also, it's best practice to switch your code based on the capability (in this case it's respondsToSelector) instead of iOS version - that's a fragile assumption (who's to say it doesn't change in the future).

You can set it for just that instance or globally for all tab bars:

// 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
}

这篇关于自定义UITabBar背景图片在iOS 5及更高版本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆