ios - ios4 中的 TabBar 自定义在 ios5 中不起作用

标签 ios xcode uitabbarcontroller customization uitabbar

在我的应用程序中,我想自定义标签栏的模式,但我的部署目标是 ios4,所以我在 appDelegate.m 的 appDidBecomeActive 中使用下面的代码来执行此操作:

CGRect frameTab = CGRectMake(0, 0, 480, 49);
UIView *viewTab = [[UIView alloc]initWithFrame:frameTab];
UIImage *tabBarBackground = [UIImage imageNamed:@"tab_bar.png"];
UIColor *tabColor = [[UIColor alloc]initWithPatternImage:tabBarBackground];
[viewTab setBackgroundColor:tabColor];
[[myTabBarController tabBar] insertSubview:viewTab atIndex:0];

当我使用 4.3 模拟器运行应用程序时,它可以正常工作,但是当我在 ios5 中模拟时,它不起作用,选项卡变回黑色。有什么帮助吗?

谢谢。

最佳答案

在 iOS 5 中,UITabBar 类中有新的 backgroundImage 属性,您应该使用它:

UIImage *tabBarBackground = [UIImage imageNamed:@"tab_bar.png"];
if ([[myTabBarController tabBar] respondsToSelector:@selector(setBackgroundImage:)]){
     [[myTabBarController tabBar] setBackgroundImage: tabBarBackground];
}
else{
    // If no backgroundImage property (pre iOS5) use your old code
    CGRect frameTab = CGRectMake(0, 0, 480, 49);
    UIView *viewTab = [[UIView alloc]initWithFrame:frameTab];
    UIColor *tabColor = [[UIColor alloc]initWithPatternImage:tabBarBackground];
    [viewTab setBackgroundColor:tabColor];
    [[myTabBarController tabBar] insertSubview:viewTab atIndex:0];
}

关于ios - ios4 中的 TabBar 自定义在 ios5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243006/

相关文章:

ios - GoogleAPI PanoView StreetView Bug - 图像加载不正确

iOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中

ios - 如何在单击按钮时在 uitabbaritem 上打开新的 View Controller

ios - objective-c 全局键字符串?

iphone - 点击背景将背景颜色更改为所选颜色

ios - LaunchScreen.xib 工具栏错误地定位 BarButtonItems

ios - 导入 RealmSwift 时不会构建带有 Swift 2.1 项目的 Realm (模块文件是由旧版本的编译器创建的)

iOS:跨viewControllers在导航栏下方添加 View

ios - AWS 如何知道要从 iOS api 连接到哪个 DynamoDB 数据库?

ios - 用atos符号化