自定义 UITabBar 背景图像在 iOS 5 及更高版本中不起作用 [英] Custom UITabBar background image not working in iOS 5 and later
问题描述
我有一段简单的代码,用于在 tabBar 上放置背景图像.
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];
这在 iOS 4 中运行良好,但在 iOS 5 中测试时,它不起作用.
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 提供了 UIAppearance 代理.
iOS5 offers the UIAppearance Proxy.
此外,最佳实践是根据功能(在本例中是 RespondsToSelector)而不是 iOS 版本来切换您的代码 - 这是一个脆弱的假设(谁说它在未来不会改变).
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屋!