【问题标题】:TabBar customization in ios4 doesnt work in ios5ios 4 中的 TabBar 自定义在 ios5 中不起作用
【发布时间】:2011-11-23 13:30:41
【问题描述】:

在我的应用程序中,我想自定义标签栏的模式,但我的部署目标是 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 xcode uitabbarcontroller customization uitabbar


    【解决方案1】:

    在 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 4 上运行不兼容,因为 if 语句会阻止 ios4 进入和设置它,对吗?
    • 但是 [[myTabBarController tabBar] insertSubview:viewTab atIndex:0];可能被认为是对系统私有部分的访问?
    • @Ondrej,它不完全是私有 API,所以它可能是 appstore 安全的。但正如您所看到的,它相当脆弱,依赖于内部实现并且可能会随着每个 ios 版本而中断,因此对于较新的 iOS 版本,您应该使用其他解决方案
    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多