简体   繁体   English

在标签栏上设置背景图像

[英]Setting a background image on a tab bar

I had the following code in place which was putting a nice image in the background of my tabbar, but its stopped working. 我有以下代码,可以在我的标签栏的背景中放置一个漂亮的图像,但是它停止了工作。 Has anyone got any other suggestions as to what I can do. 有没有人对我能做什么有其他建议。 I've tried a few other options as seen here: Setting a background image for a tabbar 我尝试了其他一些选项,如下所示: 设置标签栏的背景图片

-(void)viewDidLoad 
{
    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
    UIColor *c = [[UIColor alloc] initWithPatternImage:i];
    v.backgroundColor = c;
    [c release];
    [mainTabBar insertSubview:v atIndex:0]; 
    [v release];
    [super viewDidLoad];
}

Any help appreciated. 任何帮助表示赞赏。

UPDATE 23rd January 2012 更新2012年1月23日

Ok, I've made a bit of progress. 好的,我已经取得了一些进步。 This only stopped working since I upgraded to Xcode 4.2 and IOS5. 自从我升级到Xcode 4.2和IOS5以来,这只是停止工作。 I managed to get it back using the options in Interface Builder, but now it only works for IOS5. 我设法使用Interface Builder中的选项将其取回,但现在它仅适用于IOS5。 Ideally I would have liked to get working programatically but I'll settle for the IB solution for now. 理想情况下,我希望开始编程工作,但现在我将选择IB解决方案。

I just can't seem to get it working for any previous releases. 我似乎无法使其在任何以前的版本中都能正常工作。

NOTE: my TabBar is only on my rootviewcontroller and no other screens. 注意:我的TabBar仅在我的rootviewcontroller上,没有其他屏幕。

Regards, Stephen 问候斯蒂芬

You don't need that subview trick to style tab bars any more in iOS5 because of the new appearance APIs. 由于新的外观API,您不再需要该子视图技巧来设置iOS5中的选项卡栏样式。 You can set properties like backgroundImage and tintColor on tab bars directly in iOS 5, so just do something like this to detect if that method is available: 您可以在iOS 5中直接在标签栏上设置诸如backgroundImage和tintColor之类的属性,因此只需执行以下操作即可检测该方法是否可用:

if ([mainTabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    mainTabBar.backgroundImage = someImage;
}
else
{
    //use the old iOS4 subview hack
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM