iphone - 自定义 UITabBar 背景图像在 iOS 5 及更高版本中不起作用

标签 iphone objective-c ios cocoa-touch ipad

我有一段简单的代码可以在 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/

相关文章:

ios - 在 iOS 应用程序中将图像上传到 Facebook

ios - 处理 iPhone 5、6 和 6 Plus 的高度和宽度尺寸(磅)

ios - swift 2.0 : Cannot convert NSDateComponents to NSCalendarUnit

ios - HKActivitySummary dateComponents 落后一天

iphone - 调整 UISearchDisplayController 变暗的黑色覆盖层

iphone - iOS 如何有效地对 cgimage 执行旋转(最多 125 帧)?

ios - 去除 html 标签并在 UILabel Objective c 中显示

iphone - 带有异常肥胖的 UISegmentedControl 的 UIToolbar

ios - 如何将字符串写入文件?

ios - 无法将类型 "Any"的值分配给类型 "String?"Swift 3 (iOS)