Can I give a UIToolBar a custom background in my iPhone app? - iphone

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.

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

UIToolbar inherits from UIView. This just worked for me:
[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];

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

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

just add this piece to your -(void)viewDidLoad{}
[toolBarName setBackgroundImage:[UIImage imageNamed:#"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

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;

You can use the Appearance API since iOS5:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

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

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

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

Related

Adding image to Navigation bar in xcode?

I have added Navigation bar and Navigation Item in xib. Now I need to add image to the left of Navigation bar, but its not getting added.
This is the code I'm working on:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
I can't understand why the image is not getting added. How can I add it?
Looks like this is an old question with an accepted answer. However, I wanted to add:
In at least Xcode 5, if you are using storyboards then you can add the image as the navigationBar's title by dragging an empty view into the title area, and then placing the ImageView inside the empty view.
The following screenshot should give you an idea of how it looks:
#implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect {
CGRect currentRect = CGRectMake(0,0,100,45);
UIImage *image = [UIImage imageNamed:#"ic_launcher.png"];
[image drawInRect:currentRect];
}
#end
UPDATE
just add this code in your viewDidLoad: method..
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
and also if you want to direct set image in background of NavigationBar then use bellow line...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
also see this Link how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk
i hope this help you...
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:#"Your Image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 10, 72, 19);
[self.navViewController.navigationBar addSubview:imageView];
}
If you want to set your image at particular point in navigation bar then you can make a view and then add your image inside the view, adjust your image frame and then add this view to your navBar.
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];
If you are adding NavBar from xib then just make IBOutlet for your UINavigationBar connect it in xib then
[myNavigationBar addSubview:view];
For Swift 4:
override func viewDidAppear(_ animated: Bool) {
// 1
let nav = self.navigationController?.navigationBar
// 2
nav?.barStyle = UIBarStyle.black
nav?.tintColor = UIColor.yellow
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
// 4
let image = UIImage(named: "Apple_Swift_Logo")
imageView.image = image
// 5
navigationItem.titleView = imageView
}
Try to set the imageView.frame also please cross check the image initailized properly
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];
Your code looks ok. But I don't know why you need to add navigation bar and item in xib. If you create your own view controller derived from UIViewController, it has contained navigation bar. You may try to remove yourself added navigation bar and item from xib and see what's happen.
If you want to show nav bar on the first view of a view-based application, you need to modify your application class code as below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
// REMOVE THIS LINE !!!!!!!!!!!!!
//self.window.rootViewController = self.viewController;
// ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I just did a test like the following:
Create a new single-view based project.
Run it without any modification. It will show an empty screen.
Modify didFinishLaunchingWithOptions() as above in the application.m file.
In the viewcontroller.m, add the following lines.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view.image = [UIImage imageNamed:#"info.png"];
self.navigationItem.titleView = view;
}
Now run again. You will see a nav bar with a image as title.
To hide / show the image between movements to another controller do this. Adjust the CGRectMake dimensions accordingly.
Add this to your header file:
#property (weak, nonatomic) UIImageView *navBarLogo;
Add this to your .m controller file:
- (void)viewWillAppear:(BOOL)animated {
UIImage *image = [UIImage imageNamed:#"image_name.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(53, 7, 210, 30);
self.navBarLogo = imageView;
[self.navigationController.navigationBar addSubview:imageView];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}
I hope this will help you sure. if not work mean check your image name is correct.
UIImage *yourimage = [UIImage imageNamed: #"i_launcher.png"];
UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;
if you wish add action means :
[buttonBarButton addTarget:self action:#selector(your function) forControlEvents:UIControlEventTouchUpInside];

how to customize QLPreviewController's navBar and toolbar tintColor

QLPreviewController * preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.currentPreviewItemIndex = sender.tag;
preview.editing= YES;
[self presentModalViewController:preview animated:YES];
[preview release];
These two lines does not work for me. so be careful before writing these lines.
[preview.tabBarController.tabBar setTintColor:[UIColor blackColor]];
[preview navigationController].navigationBar setTintColor: [UIColor blackColor]];
Since iOS5 you can theme controls based on instance, globally or when contained by specific container classes. Since iOS6 the former method of subclassing QLPreviewController to set the tintColor of the UINavigationBar stopped working.
Consider one of the following as an example of a workaround that is compatible with iOS5 and iOS6:
Any UINavigationBar contained within a QLPreviewController:
[[UINavigationBar appearanceWhenContainedIn:[QLPreviewController class], nil]
setTintColor:[UIColor blackColor]];
or globally set the tintColor of all UINavigationBar instances within your app with:
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
This same strategy works with the UITabBarController.
set style of UINavigationController with this line..
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
and for change the color of TabBar just Add the below code in viewWillAppear of your class
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.6 alpha:0.8]];
[v setAlpha:0.5];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
[v release];
If you want to change the tintColor of the navigationBar you can push your QLPreviewController instead present it modally:
//i assume that you already have a navigationController
[[self navigationController] pushViewController:previewer animated:YES];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
For the bottom bar i think that is a UIToolbar not a UITabBar, probably you cant change the color (i dont know), but surely you can't call preview.tabBarController.tabBar.
I found a solution , though it is not the correct way but it works:
Make subclass of QLPreviewController
MyQLPreviewController.h
#interface MyQLPreviewController : QLPreviewController
#end
and in .m of that new subclass, copy the following code
#implementation MyQLPreviewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIToolbar *toolbar = [self getToolBarFromView:self.view]; //NOTE: Not the correct apperoach! could not think better solution, as iOS does not allow to access the toolbar properties in QLPreviewController
toolbar.barTintColor = [UIColor redColor];
}
- (UIToolbar *)getToolBarFromView:(UIView *)view
{
for (UIView *subView in view.subviews)
{
if ([subView isKindOfClass:[UIToolbar class]])
{
return (UIToolbar *)subView;
}
else
{
UIToolbar *toolBar = [self getToolBarFromView:subView];
if (toolBar)
{
return toolBar;
}
}
}
return nil;
}

Navigation Controller not working properly

I am using a navigation controller in my current application, but I had an issue with the navigation controller with the iOS4 and iOS5 so i tried to write the code for both iOS 4 & 5
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"header.png"].CGImage;
}
But problem is when I run my app on iOS 4 version my navigation Controller look like this.
please suggest me.
Thank after a long search I tried this code which helped me.
import "ImageViewController.h"
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
implement this code in your .m file
#implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
#end
UINavigationController *navControl;
In else part, try like this.
UINavigationBar *navBar = self.navControl.navigationBar;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
imgView.image = [UIImage imageNamed:#"header.png"];
[navBar addSubview:imgView];
[imgView release];

Custom UITabBar background image not working in iOS 5 and later

I have a simple piece of code that places a background image on the tabBar.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];
This works fine in iOS 4 but when testing in iOS 5, it doesn't work.
I'm trying to do the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
NSString *reqSysVer = #"4.3";
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
if ([iOSVersion compare:reqSysVer options:NSNumericSearch] !=NSOrderedDescending) {
// code for iOS 4.3 or below
[self.tabBarController.tabBar insertSubView:imageView atIndex:0];
}
else {
// code for iOS 5
[self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}
[imageView release];
Alas, this isn't working... Can anyone offer a solution?
iOS5 offers the UIAppearance Proxy.
Also, it's best practice to switch your code based on the capability (in this case it's respondsToSelector) instead of iOS version - that's a fragile assumption (who's to say it doesn't change in the future).
You can set it for just that instance or globally for all tab bars:
// not supported on iOS4
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// set it just for this instance
[tabBar setBackgroundImage:[UIImage imageNamed:#"tabbar_brn.jpg"]];
// set for all
// [[UITabBar appearance] setBackgroundImage: ...
}
else
{
// ios 4 code here
}
//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"]];
}
else
{
// ios 4 code here
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
UIImage *tabbag_image = [UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"];
UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
tabbg_view.backgroundColor = tabbg_color;
[tabBar insertSubview:tabbg_view atIndex:0];
}
After reviewing various articles, I found the answer for anyone that's having the same problem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
[self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
//iOS 4.whatever and below
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}
[imageView release];
Works like a charm! Enjoy.
I realize this has been solved, I'm posting this for others who had the same issue as me. I wanted to add a background image to the selected tab in a tabbar. Here is the solution:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar-item.png"]];
The second line here adds a background image to the selected tab in a tabbar.
There is something new in iOS 5
forBarMetrics:UIBarMetricsDefault
Here is the apple doc on that
[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];

How can I use different multiple categories for UIToolbar's?

I have a few UIToolbars throughout my app. I want each one to have a different background. Here is an example of a Category I have:
#import "UIToolbar+NavBarAdditions.h"
#implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
UIImage *barImage = [UIImage imageNamed:#"rowbg.png"];
[barImage drawInRect:rect];
}
#end
I have a few images rowbg1, rowbg2 etc etc that I want to use for other UIToolbars that are inside of my app. How can I choose which category to use for the respective toolbar?
Change a toolbar background
Using a subclass
A category changes all UIToolbar classes. If you need different toolbars, let each client be in charge of setting the image:
#interface UIBgToolbar : UIToolbar {
#private
UIImage *_background;
}
#end
#implementation UIBgToolbar
- (id)initWithFrame:(CGRect)aRect imagen:(UIImage*)image {
if (self = [super initWithFrame:aRect]){
_background = [image retain];
}
return self;
}
- (void) drawRect:(CGRect)rect {
UIImage *barImage = _background;
[barImage drawInRect:rect];
}
-(void)dealloc {
[_background release];
[super dealloc];
}
#end
Usage:
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIBgToolbar *toolbar = [[UIBgToolbar alloc] initWithFrame:rect imagen:bg];
[self.view addSubview:toolbar];
[toolbar release];
Using a subview
This is an alternative way that adds a subview and doesn't need subclasses or categories:
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:rect];
UIImageView *background = [[[UIImageView alloc] initWithImage:bg] autorelease];
background.frame = toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
[self.view addSubview:toolbar];
[toolbar release];
Making the background transparent.
Don't use this unless you really need a transparent background.
#interface UITransparentToolBar : UIToolbar
#end
#implementation UITransparentToolBar
- (void)drawRect:(CGRect)rect {
[[UIColor clearColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
#end
Usage:
// create the toolbar
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIBgToolbar *toolbar = [[UIBgToolbar alloc] initWithFrame:rect];
// add the background
// self.backgroundColor = [UIColor clearColor];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
[toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
[self.view addSubview:toolbar];
[toolbar release];
Change a navigation bar
Navigation bar background
Use Noah's code in a category.
Using a subclass is also possible:
Create a UINavigationToolbar subclass containing the drawRect method from Noah's answer. - Select your MainWindow.XIB, select "Navigation Bar", press ⌥⌘3 to (show Identity Inspector), and change the class to the class you just created.
Also while in IB, press ⌥⌘4 (show Attributes Inspector) and set a number in the field Tag. If you look at Noah's code, that number decides which image to use.
Navigation bar toolbar background
Same thing as for a normal toolbar but using a category because the navigation toolbar is read-only:
// UIToolbar.h
#interface UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect;
#end
// UIToolbar.m
#import "TransparentToolbar.h"
#implementation UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect {
[[UIColor clearColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
#end
Usage:
// bar_bottom_bumped.png is a toolbar image with transparency
UIImage *bg = [UIImage imageNamed:#"bar_bottom_bumped.png"];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = self.navigationController.toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];
[self.navigationController.toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
If you’re creating the toolbars yourself—in IB or through code—then Jano’s solution is mostly right, though I disagree with the idea of making it transparent: you should avoid doing unnecessary blending. If you need to replace the toolbars created by a navigation controller, though, you do need to use a category rather than subclassing. You can use your existing implementation; you just need to set a different tag on each of the toolbars you want to have a different appearance, then check that value in your -drawRect:. In other words, your view controller should have something like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIToolbar *toolbar = self.navigationController.toolbar;
toolbar.tag = 3; // different value for each controller, or however you want to choose the toolbar
[toolbar setNeedsDisplay];
}
and your toolbar category should have something like this:
- (void)drawRect:(CGRect)r
{
UIImage *barImage = nil;
switch(self.tag)
{
case 1:
barImage = [UIImage imageNamed:#"rowbg.png"];
break;
case 3:
barImage = [UIImage imageNamed:#"something else.png"];
break;
}
[barImage drawInRect:self.bounds];
}
You should use subclassing instead of categories.