views:

2683

answers:

3

Is it possible to give a UIToolBar a custom background from an image rather than the usual tinted blue/black fade out?

I've tried giving the view a background and setting the opacity of the UIToolBar but that also affects the opacity of any UIBarButtons on it.

+8  A: 

Answering my own question here!!! Overriding the drawRect function and creating an implementation of the UIToolbar does the trick :)

    @implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"nm010400.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
idimmu
You are creating a "category" on UIToolbar.
gerry3
This works thanks, but I think it's better to subclass the UIToolbar.
HansPinckaers
A: 

UIToolbar inherits from UIView. This just worked for me:

[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
Ajay Gautam
A: 

If you use idimmu's answer and want your barbuttonitems to be colored instead of the defaults, you can add these couple of lines of code as well to your category:

UIColor *color = [UIColor redColor];
self.tintColor = color;
dnstevenson