2

In order to create an absolute bottomed footer on top of a tableView I found that using UIToolbar for this and adding custom views for it worked fine.

My problem is that I already use this as a toolbar for a webview, and here with another background image than I need now.

By replacing the drawRect function in UIToolbar+addition.m I have a global toolbar for this that works fine in my webviews.

How can I expand this so that I can select which version(background) to use the different places?

My UIToolbar+addition.m:

#import "UINavigationBar+addition.h"

@implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
    UIImage *barImage = [UIImage imageNamed:@"toolbar-bg.png"];
    [barImage drawInRect:rect];
}
@end
doh
  • 387
  • 1
  • 4
  • 15

2 Answers2

0

Try creating separate .h and .m files for each "version", and import the appropriate .h into the class file you'd like it to affect.

Aaron Ash
  • 1,412
  • 13
  • 24
0

Why not just add a barImage property to your extension?

@interface UIToolbar (Addition)

@property (nonatomic, retain) UIImage *barImage;

@end

Then, in your implementation (I'm doing this assuming you're not using ARC. If you are, obviously remove the retain/release stuff):

@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

Now, all you have to do is set the barImage property after instantiating your UIToobar. e.g.:

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

And, if you want to change it after it's already onscreen, you can do so by just setting the barImage property to a new UIImage.

Looks like it's been a year since this question was posted, but hopefully this might help someone.

Ashwin
  • 2,317
  • 1
  • 15
  • 5