0

I'm having trouble setting the background image on my tabbar. I've upgraded to iOS5 and Xcode 4.2, and the tabbar background image just stopped working.

The tabbar is only present on my RootViewController.

I managed to use the Interface Builder and applied a background colour which is not exactly what I wanted but will do for now. This only works for iOS5 for some reason. Ideally I would like to do it programmatically. I'm trying to get the following code working:

RootViewController.h

@interface RootViewController : UIViewController {

    IBOutlet UITabBar *mainTabBar;
    IBOutlet UITabBarItem *settingsBarItem;
    IBOutlet UITabBarItem *infoBarItem;
    IBOutlet UITabBarItem *aboutBarItem;

}

@property (retain,nonatomic) UITabBar *mainTabBar;

@end

RootViewController.m

-(void)viewDidLoad {

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallMenuBackground.png"]]; 
        if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
            mainTabBar.backgroundImage = imageView;
        }
        else{
            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];
        }
}

This code is just not working for me, I must be doing something wrong, so any help appreciated.

I'm getting a couple of warning messages aswell:

1) Instance method '-respondToSelect:' not found (return type defaults to 'id')

2) Incompatible pointer types assigning to 'UIImage *' from 'UIImageView *'

EDIT

The entire code is too large to paste here, the RootViewController.h is http://pastie.org/3249124 and the RootViewController.m is http://pastie.org/3249137

Stephen
  • 4,715
  • 8
  • 53
  • 78
  • Looks like a similar issue to this - http://stackoverflow.com/questions/7800474/custom-uitabbar-background-image-not-working-in-ios-5 – FractalDoctor Jan 25 '12 at 10:39
  • I can help you with the 2nd warning, you should assign an image to this line `mainTabBar.backgroundImage = imageView;` , maybe try `mainTabBar.backgroundImage = [UIImage imageNamed:@"smallMenuBackground.png"];` – iNoob Jan 25 '12 at 10:39
  • All, please see the following post for a work around. http://stackoverflow.com/questions/8909379/setting-a-background-image-for-a-tabbar – Stephen Feb 01 '12 at 11:59
  • Please see the following link to the answer: http://stackoverflow.com/questions/8909379/setting-a-background-image-for-a-tabbar – Stephen Feb 01 '12 at 11:59

2 Answers2

2

this line, shouldn't it be

if ([mainTabBar respondsToSelector:@selector(setBackgroundImage:)])

respondsToSelector: not respondsToSelect:

also, just as it said exactly,

mainTabBar.backgroundImage = imageView;

should be

mainTabBar.backgroundImage = [UIImage imageNamed:@"smallMenuBackground.png"];

because backgroundImage is UIImage while your imageView is UIImageView

Edit

also, I find that the code in your else statement is kind of weird, try if this is working.

else
{
    UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
    UIImageView* imageView = [UIImageView alloc] initWithImage:i];
    [mainTabBar insertSubview:imageView atIndex:0]; 
    [imageView release];
}

EDIT 2:

    if ([mainTabBar respondsToSelect:@selector(setTintColor:)])
    {
        UIImage *i = [UIImage imageNamed:@"smallMenuBackground.png"];
        UIColor* c = [[UIColor alloc] initWithPatternImage:i];
        mainTabBar.tintColor = c;
    }
    else
    {
        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];
    }
X Slash
  • 4,133
  • 4
  • 27
  • 35
  • Thanks X Slash, that got rid of the warnings, but I'm still having the problem with the background image not appearing as expected. – Stephen Jan 25 '12 at 10:47
  • oops, sorry, I just assumed your code (logic) is working but there are syntax errors. – X Slash Jan 25 '12 at 10:55
  • Thanks X Slash, bit still no luck, that didn't work. I've edited my original post to link to full code listing. – Stephen Jan 25 '12 at 11:16
  • sorry, I hope this last bit is working for you, if not, I don't have anything to offer you anymore – X Slash Jan 25 '12 at 11:37
  • hi i want to set image to tabBar icon...image is colored image... how can i do this, if the image is black and white then i don't have any problem but its creating problem with colored image – Rajneesh071 Oct 06 '12 at 09:59
1

you have minor problem you pass imageview in mainTabBar.backgroundImage it's worng , you will pass UIImage ... please check this code

-(void)viewDidLoad {
            if ([mainTabBar respondsToSelect:@selector(setBackgroundImage:)]){
                mainTabBar.backgroundImage =  [UIImage imageNamed:@"smallMenuBackground.png"] ;
            }
            else{
                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];
            }
    }
Deepesh
  • 633
  • 4
  • 11