Stephen
Stephen

Reputation: 4813

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

Ok, I've made a bit of progress. This only stopped working since I upgraded to Xcode 4.2 and IOS5. I managed to get it back using the options in Interface Builder, but now it only works for IOS5. Ideally I would have liked to get working programatically but I'll settle for the IB solution for now.

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.

Regards, Stephen

Upvotes: 0

Views: 3684

Answers (1)

Nick Lockwood
Nick Lockwood

Reputation: 41005

You don't need that subview trick to style tab bars any more in iOS5 because of the new appearance APIs. 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:

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

Upvotes: 3

Related Questions