
Untitled
By: a guest on
Jun 30th, 2012 | syntax:
None | size: 1.29 KB | hits: 11 | expires: Never
Multiple custom backgrounds for UIToolbar
#import "UINavigationBar+addition.h"
@implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
UIImage *barImage = [UIImage imageNamed:@"toolbar-bg.png"];
[barImage drawInRect:rect];
}
@end
@interface UIToolbar (Addition)
@property (nonatomic, retain) UIImage *barImage;
@end
@implementation UIToolbar (Addition)
@synthesize barImage = _barImage;
//Override barImage setter to force redraw if property set after already added to superView...
- (void)setBarImage:(UIImage *)barImage {
if (_barImage != barImage) {
UIImage *oldBarImage = [_barImage retain];
_barImage = [barImage retain];
[oldBarImage release];
//Let this UIToolbar instance know it needs to be redrawn in case you set/change the barImage property after already added to a superView...
[self setNeedsDisplay];
}
}
- (void) drawRect:(CGRect)rect {
[self.barImage drawInRect:rect];
}
//If you're not using ARC...
- (void)dealloc {
[barImage release];
[super dealloc];
}
@end
UIToolBar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //Or whatever frame you want...
myToolbar.barImage = [UIImage imageNamed:@"toolbar-bg.png"];
[self.view addSubView:myToolbar];
[myToolbar release];