Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Multiple custom backgrounds for UIToolbar
  2. #import "UINavigationBar+addition.h"
  3.  
  4. @implementation UIToolbar (Addition)
  5. - (void) drawRect:(CGRect)rect {
  6.     UIImage *barImage = [UIImage imageNamed:@"toolbar-bg.png"];
  7.     [barImage drawInRect:rect];
  8. }
  9. @end
  10.        
  11. @interface UIToolbar (Addition)
  12.  
  13. @property (nonatomic, retain) UIImage *barImage;
  14.  
  15. @end
  16.        
  17. @implementation UIToolbar (Addition)
  18.  
  19. @synthesize barImage = _barImage;
  20.  
  21. //Override barImage setter to force redraw if property set after already added to superView...
  22. - (void)setBarImage:(UIImage *)barImage {
  23.     if (_barImage != barImage) {
  24.         UIImage *oldBarImage = [_barImage retain];
  25.         _barImage = [barImage retain];
  26.         [oldBarImage release];
  27.  
  28.         //Let this UIToolbar instance know it needs to be redrawn in case you set/change the barImage property after already added to a superView...
  29.         [self setNeedsDisplay];
  30.     }
  31. }    
  32.  
  33. - (void) drawRect:(CGRect)rect {
  34.     [self.barImage drawInRect:rect];
  35. }
  36.  
  37. //If you're not using ARC...
  38. - (void)dealloc {
  39.     [barImage release];
  40.     [super dealloc];
  41. }
  42.  
  43. @end
  44.        
  45. UIToolBar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //Or whatever frame you want...
  46. myToolbar.barImage = [UIImage imageNamed:@"toolbar-bg.png"];
  47. [self.view addSubView:myToolbar];
  48. [myToolbar release];