Stephen
Stephen

Reputation: 4813

Cannot set background image for tabbar

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

Upvotes: 0

Views: 2546

Answers (2)

Deepesh
Deepesh

Reputation: 643

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];
            }
    }

Upvotes: 1

X Slash
X Slash

Reputation: 4131

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];
    }

Upvotes: 2

Related Questions