在我的应用程序中,我想自定义tabbar的模式,但我的部署目标是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中模拟时,它不起作用,标签会变回黑色..任何帮助?
感谢。
答案 0 :(得分: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];
}