0

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

Community
  • 1
  • 1
Stephen
  • 4,715
  • 8
  • 53
  • 78
  • Line add before image color set self.view.backgroundColor = [UIColor clearColor]; – Amit Jan 18 '12 at 15:58
  • Amit, this clears the background colour of the main view. Its the tabbar I'm interested in. – Stephen Jan 18 '12 at 16:01
  • 1
    I believe Apple may have changed the internal view layout of tab bars in iOS 5.0.1 (or otherwise intentionally broken this trick) as some other people have been reporting the same issue. The answer is to switch to using the official appearance API functions though (see my answer below). – Nick Lockwood Jan 18 '12 at 16:23

1 Answers1

3

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
}
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • Nick, I'm getting an error: Property 'backgroundImage' not found on object of type 'UITabBar *'. – Stephen Jan 18 '12 at 16:32
  • Are you building against the iOS5 SDK? Check that your base SDK in your target build settings is iOS 5.0. (It's fine if your deployment target is 4.x). – Nick Lockwood Jan 18 '12 at 17:20
  • Nick, my building settings are correct. Unfortunately I couldn't get this to work. – Stephen Jan 25 '12 at 09:56
  • Well according to the docs, UITabBar has a property called backgroundImage that was added in iOS5, so if you're building for iOS5 then something very weird is going on: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UITabBar_Class/Reference/Reference.html – Nick Lockwood Jan 25 '12 at 10:11
  • Try replacing it with [maintabBar setBackgroundImage: someImage] instead. You may get a warning but at least it will compile and run. I still think the most likely explanation is that you've not set your build target to iOS5, but I can't really prove that without seeing your project! – Nick Lockwood Jan 25 '12 at 10:12