Change background image of UIToolbar - iphone

How can I change the background image of UIToolbar? I have been able to do this for navigation bar, but not UIToolbar. I do not want to change tintColor. I would like to change the whole image.
Thanks for your help.

From Can I give a UIToolBar a custom background in my iPhone app?
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

You could use the following code:
[myToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"toolbar_40.png"]] autorelease] atIndex:0];
Fixed for Simulator 4.3.

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

Related

Navigation bar image black

In my application i add custom navigation image while adding the remaining part of the image shows black in color, i used following code,
UIImage *image = [UIImage imageNamed: #"navbar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarStyleDefault];
i want to show the back ground color in the in the black color part.what should change in code?
Please Try Following may be it works fine.
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:#"navbar.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
Edited
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"navbar.png"]];
I'm using very useful library (class SCNavigationBar) for styling navigaitonBar, download it here
It supports all orientations, it works with no problem in my many apps.

Changing the UINavigationBar background image

I've been trying to change the background image of the UINavigationBar of my application. I tried several ways. First I added to my AppDelegate class the following code:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
But it wasn't working. My next try was to write a CustomizedNavigationBar Class which is overriding the drawRect method. It looked like that:
CustomizedNavigationBar.h
#import <UIKit/UIKit.h>
#interface CustomizedNavigationBar : UINavigationBar
#end
CustomizedNavigationBar.m
#import "CustomizedNavigationBar.h"
#implementation CustomizedNavigationBar
- (void) drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
NSLog(#"I got called!!!!!");
}
#end
In my .xib file where the NavigationBar is defined I changed the class to the new CustomizedNavigationBar. But still it is not working..
As another test I downloaded an example project where the background image should be changed. But even with that sample code it was not working.
What am I doing wrong? I am using IOS 5. Any suggestions or other ways I could define a background image?
Thanks for your answers!
Starting in iOS 5 you should use the -setBackgroundImage:forBarMetrics: method:
[myNavbar setBackgroundImage:[UIImage imageNamed: #"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
And in Swift 4:
navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
for: .default)
Considering all iOS versions, this seems to be accomplishing both Custom background image and Custom size of UINavigationBar:
#interface CustomNavigationBar : UINavigationBar
#end
#implementation CustomNavigationBar
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"navigationBar"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
//for iOS5
[self setBackgroundImage:[UIImage imageNamed: #"navigationBar"] forBarMetrics:UIBarMetricsDefault];
}
//for custom size of the UINavigationBar
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(320,31);
return newSize;
}
#end
I use such codes in a common place like a Utilities file.
Hope this helps.
Just try with this code.. In your implmentation (.m) file.
#import "RootViewController.h"
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navheader.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title=#"You are Done.";
}
This has worked in excellent way for me.
Above Code Worked for only IOS 4. if you use the IOS 5 then use.....
[myNavbar setBackgroundImage:[UIImage imageNamed:
#"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
You can add UIImageview to navigation bar as follows
UINavigationBar navBar = [[UINavigationBar alloc]init];
[navBar addSubview: imageview];
[navBar release];
You can check the post :
iPhone - NavigationBar Custom Background
You can also try this for Navigationbar Background.
UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: #"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
Thanks.
Another way to set an image in navigation bar is,
UIImage* logo = [ UIImage imageNamed:#"Logo.png" ];
UIImageView* logoView = [[[UIImageView alloc] initWithImage:logo] autorelease];
self.navigationItem.titleView = logoView;
This will not change whole navigation bar background. But adds a background image centered on the navigation bar, which is sometime more intended (what I was looking for).
Well for iOS 4 there is a simple solution, like:
nvc.navigationBar.layer.contents = (id)img.CGImage;

Transparent UIToolbar

I wrote the following code to make my toolbar transparent.
[mtoolbar setBackgroundColor:[UIColor clearColor]];
How do I make UIToolbar transparent?
You can set the property translucent to YES and see if this helps.
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
Setting the property translucent to YES will not work in iOS 5 and below. Here's how it can be done without subclassing toolbar:
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Check the below code
[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];
Taken from
#Brandon Bodnár has answered in the below SO post.
Couldn't UIToolBar be transparent?
you could also use the different approach
Transparent UIToolBar
for (UIView * sv in [toolBar subviews])
{
[sv removeFromSuperview];
}
;) any iOs
The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).
Please note:
Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.
TransparentToolbar.h
#import <UIKit/UIKit.h>
#interface TransparentToolbar : UIToolbar
#end
TransparentToolbar.m
#import "TransparentToolbar.h"
#implementation TransparentToolbar
-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
// This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
if (index != 0)
{
[super insertSubview:view atIndex:index];
}
else
{
// insert your custom background view, if you want to
}
}
#end
EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.
[someToolbar setBackgroundImage:[UIImage imageNamed:#"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
This worked for me for iOS 6 and 7:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

change navigation bar background using my own image

i want to change navigation bar background using my own image, but i really dont know what to do. i was search it on google, but some samples tell in navigationbar template. my application is a split view base. How could i do that??
If you want to change the background of the navigation bar that too without using category, this might be helpful to give that feel, see if it fulfills your need:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
//here for v, width= navBar width and height=navBar height
[v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"a.png"]]];
[self.view addSubview:v];
[v release];
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
}
#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
You can add the image directly in the navigation bar background with the code below
[navigation_bar setBackgroundImage:[UIImage imageNamed:#"image.jpg"] forBarMetrics:UIBarMetricsDefault];
To add a background image to UINavigationBar, you need to create a category that extends UINavigationBar. Just find the below given code and add it into your implementation file.
#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
After implementing it.. you can call it anywhere within your application and you can see the change on every view. Do not forget to add image into your resources folder.

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