Setting UITabBarIcon tint when unselected - iphone

I am trying to set the icons of a UITabBarItem, but it is not working. By the way, I'm using Xcode 5 Beta for this project.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
{
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
UIImage *tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
}
return YES;
}
I am trying to get the icons to be white when both selected and unselected, but they are remaining gray in the unselected state.
Edit
I tried doing this, but now im getting the error "Expression Result Unused"
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
tabBarItem1.title = #"Home";
UIImage *graph = [UIImage imageNamed:#"graph.png"];
[tabBarItem1 initWithTitle:(NSString *)#"HELLO" image:(UIImage *)graph selectedImage:(UIImage *)graph];

tintColor sets the tint color of the background, you can't see this if you use a custom background image. If you don't want to have the system default gradient you have to use setFinishedSelectedImage:withFinishedUnselectedImage: and set your white image for both of them.
Like this:
UIImage *selectedImage = ...
UIImage *unselectedImage = selectedImage;
[tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
If you are setting those images in a Storyboard you can iterate over the existing UITabBarItems and change their images.
Put something like this into application:didFinishLaunchingWithOptions:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSAssert([tabBarController isKindOfClass:[UITabBarController class]], #"self.window.rootViewController must be a tabBarController");
for (UIViewController *viewController in tabBarController.viewControllers) {
UITabBarItem *tabBarItem = viewController.tabBarItem;
UIImage *tabImage = tabBarItem.image;
[tabBarItem setFinishedSelectedImage:tabImage withFinishedUnselectedImage:tabImage];
}

Related

UITabBar tint in iOS 7

How can i specify the tint of images when a tab is selected and unselected?
I have tried this but it doesnt work:
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
This makes the selected image tint red(not green) and unselected tint gray (not red).
You can set the tint color for selected and unselected tab bar buttons like this:
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
The first line sets the unselected color - red in this example - by setting the UIView's tintColor when it's contained in a tab bar. Note that this only sets the unselected image's tint color - it doesn't change the color of the text below it.
The second line sets the tab bar's selected image tint color to green.
Are you using the template-version of your images?
Instead of setting your images with [UIImage imageNamed: #"MyImage"], set them with [[UIImage imageNamed: #"MyImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate].
This setup along with your code should solve this issue.
You have to use the new Image rendering modes introduced in iOS 7 (UIImageRenderingModeAlwaysOriginal and UIImageRenderingModeAlwaysTemplate )see my answer to a similar question:
Hope this helps
if you do not have many viewcontrollers. Here is my way to do it.
In your delegate method just place your tabbar bg Image. And set the UIImageView
Create UITabbar intance in AppDelegate.h
#property (nonatomic,retain) UITabBar *tabbar;
And
#synthesize tabbar;
UITabBarController *tabBarController =
(UITabBarController *)self.window.rootViewController;
tabbar = [tabBarController tabBar];
[tabbar setBackgroundImage:[UIImage imageNamed:#"tabbarBg.png"]];
NSArray *tabImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tab1Hover.png"],
[UIImage imageNamed:#"tab2.png"],
[UIImage imageNamed:#"tab3.png"],
[UIImage imageNamed:#"tab4.png"],
[UIImage imageNamed:#"tab5.png"],
nil];
for (int i = 0; i<5; i++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20+i*60+i*3.5, 10, 25, 21)];
[image setContentMode:UIViewContentModeScaleAspectFit];
[image setImage:[tabImageArray objectAtIndex:i]];
[image setTag:10+i];
[tabbar addSubview:image];
}
Then every ViewController in tabbar add
-(void)viewWillAppear:(BOOL)animated
delegate method and in this method. You can change the Imageviews as shown below.
-(void)viewWillAppear:(BOOL)animated{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = (UITabBarController *)appDelegate.window.rootViewController;
NSArray *tabImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tab1Hover.png"],
[UIImage imageNamed:#"tab2.png"],
[UIImage imageNamed:#"tab3.png"],
[UIImage imageNamed:#"tab4.png"],
[UIImage imageNamed:#"tab5.png"],
nil];
for (int i = 0; i<5; i++) {
UIImageView *image = (UIImageView*)[tabbar viewWithTag:10+i];
[image setImage:[tabImageArray objectAtIndex:i]];
}
}
So, just costumize tabImageArray in every View controller. Then you can use it.
I works on iOS 7 as well.

iOS - remove UIImageView from MFMailComposeViewController

My ViewController has a navigationBar with Promt and on top of it I've added a custom image:
- (void)changeNavBar
{
UIImage *image = [UIImage imageNamed: #"logo_home.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeLeft;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"logo_home.png"] forBarMetrics:UIBarMetricsDefault];
imageView.frame = CGRectMake(0, 0, 320, 70);
[self.navigationController.navigationBar insertSubview:imageView atIndex:0];
self.navigationController.navigationBar.backItem.backBarButtonItem.tintColor = [UIColor blackColor];
}
But when user clicks "Send Email" button, I create MFMailComposeViewController and present it modally.
- (IBAction)emailPressed:(id)sender
{
NSString *emailTitle = event.title;
NSString *messageBody = [NSString stringWithFormat:#"%# - %#", event.title, event.concertUrl.absoluteString];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
NSLog(#"subviews: %d", mc.navigationBar.subviews.count);
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
// Present mail view controller on screen
[self presentModalViewController:mc animated:YES];
}
MFMailComposeViewController's navigation bar autoresizes to normal navigationBar without prompt, but my custom image from previous VC doesn't disappear. How can I remove it and set standard Black style to my navigationBar ?
You have used UIAppearance proxy to set background image which will have effect on every navigation bar in app.
Change
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"logo_home.png"] forBarMetrics:UIBarMetricsDefault];
with
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"logo_home.png"] forBarMetrics:UIBarMetricsDefault];

Customizing QLPreviewController

I have an issue in customizing the appearance of my QLPreviewController.
We can display a QLPreviewController by pushing it in a navigation controller, or presenting it in a ModalViewController. Since my navigationController's bar is customized a little (tintColor), I'm pushing the QLPreviewController to preserve my color scheme. But when I push it, the QLPreviewController seems to have some problems : I need to systematically call [qlpvc reloadData] so that my file is displayed.
In iOS [REDACTED], even with reloadData, nothing displays in the pushing way, (actually it displays but in a random way). So I decided it could be interesting to only use the reliable Modal way.
Soooo my point is that I want to present my QLPreviewController in a ModalViewController. It works great that way, but I can't customize the viewController appearance.
For example in a didSelectRowAtIndexPath if I do :
(I don't have my sources near to me so excuse me if I do a mistake)
QLPreviewController *qlpvc = [[QLPreviewController alloc] init];
qlpvc.dataSource = self; // Data Source Protocol & methods implemented of course
No need for delegate in my case so //qlpvc.delegate = self;
qlpvc.currentPreviewItemIndex = [indexPath.row];
// The following doesn't work :
[qlpvc.navigationController.navigationBar setTintColor:[UIColor redColor]];
// The following doesn't work too :
[qlpvc.modalViewController.navigationController.navigationBar setTintColor:[UIColor redColor]];
[self presentModalViewController:qlpvc animated:YES];
[qlpvc release];
tl ; dr version : How to manage to customize my modal QLPreviewController's appearance ? Especially the tintColor of the navigationBar ?
Thanks a lot.
This works, but I don't know if it will be rejected by Apple as it's not a published method and may break in future versions of the OS. Works in iOS6.
Add to the preview controller datasource method:
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
for (id object in controller.childViewControllers)
{
if ([object isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = object;
navController.navigationBar.tintColor = [UIColor colorWithRed:0.107 green:0.360 blue:0.668 alpha:1.000];
}
}
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:#"MyPDFFile" ofType:#"pdf"];
return [NSURL fileURLWithPath:pathToPdfDoc];
}
Subclass QLPreviewController and change the tintColor, et al in viewDidLoad:.
If you are trying to maintain simple styling such as tintColor throughout your app, you should consider using UIAppearance selectors on many UIView classes. The following example customizes all instances of UINavigationBar, including those displayed in QLPreviewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//..
[self initAppearance];
return YES;
}
-(void)initAppearance{
UINavigationBar* defaultNavigationBar = [UINavigationBar appearance];
UIImage *backgroundImage = [UIImage imageNamed:#"MY_IMAGE.png"]
NSDictionary *defaultNavigationBarDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Futura-Medium" size:19], NSFontAttributeName,
[UIColor blueColor], UITextAttributeTextColor,
[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 2.0f)], UITextAttributeTextShadowOffset,
nil];
defaultNavigationBar.titleTextAttributes = defaultNavigationBarDictionary; //iOS5
//[defaultNavigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; //iOS5
[defaultNavigationBar setBarTintColor:[UIColor redColor]]; //iOS7
[defaultNavigationBar setShadowImage:[[UIImage alloc] init]]; //iOS6, removes shadow
[defaultNavigationBar setTitleVerticalPositionAdjustment:0.0f forBarMetrics:UIBarMetricsDefault]; //iOS5
[defaultNavigationBar setBackIndicatorImage:[UIImage imageNamed:#"BACK_ARROW.png"]]; //iOS7
[defaultNavigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"BACK_ARROW.png"]]; //iOS7
}

Adding image to navigation bar

I'd like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompanying UITableView. I've seen some examples of how this might work.
Set navigation bar title:
UIImage *image = [UIImage imageNamed:#"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];
The problem there is it only covers the title rather than the entire navigation bar.
There is also this thread: http://discussions.apple.com/message.jspa?messageID=9254241#9254241. Towards the end, the solution looks to use a tab bar, which I'm not using. It is that complicated to set a navigation bar background? Is there some other simpler technique?
I'd like to have a background for the navigation and still be able to use title text.
In your case, this solution found in another answer would work well.
With the "CustomImage" category added to UINavigationBar,
you can then just call:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];
This code should go in the method
- (void)viewWillAppear:(BOOL)animated
of the view controller where you want to have the custom image.
And, in that case you should better call:
[navBar clearBackgroundImage]; // Clear any previously added background image
before setBackgroundImage (otherwise it will be added multiple times...)
its changed for ios6, to make it work in ios 6 use:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:#"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
There is actually a much easier way to add a background image to any UIView class or subclass. It requires no class categorization or extension (subclassing), and you can do this on an "as needed" basis. For example, to add a background image to a view controller's navigation bar, do the following:
self.navigationController.navigationBar.layer.contents = (id)[UIImage
imageNamed:#"background.png"].CGImage;
You'll need to remember to add the Quartz Core framework to your project and add #import <QuartzCore/QuartzCore.h> wherever you need to do this. This is a much cleaner, simpler way to alter the drawing layer of anything that inherits from UIView. Of course, if you want to accomplish a similar effect for all navigation bars or tab bars, then subclassing makes sense.
UIImage *logo = [UIImage imageNamed:#"my_logo"];
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 320, 37);
UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;
//searchNavCtrl.navigationItem.titleView = logoView;
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
[searchNavCtrl.navigationBar addSubview:logoView];
[logoView release];
Just add this line .
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
Bam! One line and done.
I used cmp's solution and added some logic to remove it as I only wanted a custom background image on home screen within on view appear.
HomeViewController.m
UIImage *image = [UIImage imageNamed:#"HomeTitleBG.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
[testImgView removeFromSuperview];
} else {
NSLog(#"%s no there isn't a bg image so add it ", __FUNCTION__);
}
[self.navigationController.navigationBar addSubview:imageView];
[imageView release];
I also tried to use the suggested clearBackgroundImage method but couldn't get it to work so I gave the image a tag and then removed it in the other viewcontrollers on view will appear.
OtherViewController.m
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it", __FUNCTION__);
[testImgView removeFromSuperview];
}
`
just go the view controller and paste in super viewdidload
and replace your image in mainlogo and then set the navigation title in set your image logo
- (void)viewDidLoad
{
[super viewDidLoad];
//set your image frame
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ;
//set your image logo replace to the main-logo
[image setImage:[UIImage imageNamed:#"main-logo"]];
[self.navigationController.navigationBar.topItem setTitleView:image];
}
Add code in appdelegate did finish with launching method
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 0);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:#"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}

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

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