25

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.

RusHughes
  • 1,191
  • 2
  • 12
  • 17

10 Answers10

40

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
RusHughes
  • 1,191
  • 2
  • 12
  • 17
16

UIToolbar inherits from UIView. This just worked for me:

[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
Ajay Gautam
  • 997
  • 12
  • 14
10

Slightly modified version of loreto's answer, which works for me on ios 4 and 5:

// Set the background of a toolbar
+(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar {   
    // Add Custom Toolbar
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
    iv.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
    iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    // Add the tab bar controller's view to the window and display.
    if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
        [toolbar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
    else
        [toolbar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
    toolbar.backgroundColor = [UIColor clearColor];
}
Chris
  • 39,719
  • 45
  • 189
  • 235
  • 2
    In iOS 5 there are new APIs that allow the customization of the controls. There is no need to insert a subview. Also, checking for the system version is not the recommended practice, respondsToSelector is a much more robust alternative. – Fabio Aug 30 '11 at 19:06
  • I don't see anything wrong with checking the system version. How is respondsToSelector more robust? – Arie Litovsky Sep 28 '11 at 18:05
  • @diablosnuevos - if you check for system version now, you will always have to keep modifying your code when new versions release. respondsToSelector is independent from system version. That said, this method will respond to insertSubview regardless of version, so that respondsToSelector is not appropriate here. – Jon Dec 29 '11 at 19:54
9

This is the approach I use for iOS 4 and 5 compatibility:

if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
    [toolbar setBackgroundImage:[UIImage imageNamed:@"toolbar-background"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
} else {
    [toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar-background"]] autorelease] atIndex:0];
}
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • Hi Christopher, I am implementing this solution but I still get to see the highlight on the toolbar (that Apple standard highlight over the top half of the toolbar). Do you know if there is a way to switch off that highlight? – Bocaxica Apr 12 '12 at 10:38
  • @MadOxyn strange, I don't see that highlight. What iOS version do you see this on? – Christopher Pickslay Apr 18 '12 at 01:33
  • I was seeing it on both iOS4 & 5, but I got the issue now. I was stupid, there is a paging control laying over it with a semi-transparent white background which is causing that highlight. I didn't realize this. Your reply made me think again ;) Thanks. – Bocaxica Apr 20 '12 at 16:57
7

just add this piece to your -(void)viewDidLoad{}

[toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Mateus
  • 2,640
  • 5
  • 44
  • 62
2

You can use the Appearance API since iOS5:

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Jasper
  • 7,031
  • 3
  • 35
  • 43
2

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
  • 697
  • 7
  • 16
1

To be iOS 5 compliant you can do something like this

-(void) addCustomToolbar {

    // Add Custom Toolbar
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customToolbar.png"]];
    img.frame = CGRectMake(-2, -20, img.frame.size.width+4, img.frame.size.height);

    // Add the tab bar controller's view to the window and display.

    if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"5.0" ) )
       [self.tabBarController.tabBar insertSubview:img atIndex:1]; // iOS5 atIndex:1
    else
      [self.tabBarController.tabBar insertSubview:img atIndex:0]; // iOS4 atIndex:0

    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];

    // Override point for customization after application launch.
    [self.window addSubview:tabBarController.view];

}
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
0

You can do this with a category that basically adds a new property to UIToolBar. Overriding drawRect can work but it's not necessarily future proof. That same strategy for custom UINavigationBar stopped working with iOS 6.

Here's how I'm doing it.

.h file

@interface UIToolbar (CustomToolbar)

@property (nonatomic, strong) UIView *customBackgroundView;

@end

.m file

#import "CustomToolbar.h"
#import 

static char TIToolbarCustomBackgroundImage;

@implementation UIToolbar (CustomToolbar)

- (void)setCustomBackgroundView:(UIView *)newView {
    UIView *oldBackgroundView = [self customBackgroundView];
    [oldBackgroundView removeFromSuperview];

    [self willChangeValueForKey:@"tfCustomBackgroundView"];
    objc_setAssociatedObject(self, &TIToolbarCustomBackgroundImage,
                             newView,
                             OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"tfCustomBackgroundView"];

    if (newView != nil) {
        [self addSubview:newView];
    }
}

- (UIView *)customBackgroundView {
    UIView *customBackgroundView = objc_getAssociatedObject(self, &TIToolbarCustomBackgroundImage);

    return customBackgroundView;
}

@end

In your view controller code, e.g. viewDidLoad

    if (self.navigationController.toolbar.customBackgroundView == nil) {
        self.navigationController.toolbar.customBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_background.png"]];
        self.navigationController.toolbar.customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
Jared Egan
  • 1,268
  • 1
  • 12
  • 19
0

this one works fine for me:

ToolbarOptions *tbar = [[ToolbarOptions alloc] init];
[tbar setToolbarBack:@"footer_bg.png" toolbar:self.toolbarForPicker];
[tbar release];

#import <Foundation/Foundation.h>
@interface ToolbarOptions : NSObject {

}
-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;
@end

#import "ToolbarOptions.h"


@implementation ToolbarOptions

-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {   
// Add Custom Toolbar
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Add the tab bar controller's view to the window and display.
if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
    [bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
else
    [bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
bottombar.backgroundColor = [UIColor clearColor];
}

@end
Melvin
  • 3,421
  • 2
  • 37
  • 41