我有一段简单的代码可以在 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];
唉,这行不通...有人可以提供解决方案吗?
最佳答案
iOS5 提供了 UIAppearance 代理。
此外,最佳做法是根据功能(在本例中为 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
}
关于iphone - 自定义 UITabBar 背景图像在 iOS 5 及更高版本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800474/