I want to put background image for navigation bar. I tried the following code but it hides the title for navigation.
self.title = #"My title";
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"top.png"]];
[navBar addSubview:image];
[navBar sendSubviewToBack:image];
Any buddy know why is it so?
This should go where your navigation controller is initialized, i.e. MainViewController.m
// Set the background image of the navigation bar
UIImage *image = [UIImage imageNamed:#"banner-with-logo.png"];
[self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"background.png"] forBarMetrics:UIBarMetricsDefault];
You can done by override the drawRect method
#implementation UINavigationBar (TENavigationBar)
- (void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"navigation_background"];
[image drawInRect:CGRectMake(0,
0,
self.frame.size.width,
self.frame.size.height)];
}
#end
Solution by WaiLam will work but if you are playing movies in your app the player navigation bar will also get change. Here is a better approach -
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if([self isMemberOfClass:[UINavigationBar class]])
{
UIImage *image;
image=[UIImage imageNamed:#"hor_bar"];
CGContextClip(ctx);
CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx,
CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
else
{
[super drawLayer:layer inContext:ctx];
}
}
#end
This will add an image, with the correct sizing, to the NavigationBar, centered.
/* custom navbar logo, centered with same height as navbar */
UIImageView *logo = [[UIImageView alloc] initWithFrame:
CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,
self.navigationController.navigationBar.frame.size.height-1)];
[logo setImage:[UIImage imageNamed:#"text_logo.png"]];
[self.navigationController.navigationBar addSubview:logo];
[self.navigationController.navigationBar sendSubviewToBack:logo];
If you want to add the image so that it is always the same throughout the application than use this:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: #"background-image.png"];
[img drawInRect:CGRectMake(0,
0,
self.frame.size.width,
self.frame.size.height)];
}
otherwise if you want to set the image at a particular screen only than it is bit tricky like:
- (void) viewWillAppear{
...
...
[self addNavigationBarImage:YES];
}
- (void) viewWillDisappear{
...
...
[self addTheNavigationBarImage:NO];
}
- (void) addTheNavigationBarImage:(BOOL)add{
UIImageView *imgView = (UIImageView *)[[self.navigationController navigationBar] viewWithTag:TAG];
if(add){
if(imgView == nil){
imgView = [[[UIImageView alloc] initWithImage:IMAGE] autorelease];
[self.navigationController.navigationBar insertSubview:imgView atIndex:0];
}
//EDIT:
else{
[self.navigationController.navigationBar sendSubviewToBack:imgView];
}
}
else{
if(imgView)
[imgView removeFromSuperview];
}
}
Hope this helps.
Related
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];
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;
In my app I am overriding the uinavigationbar color in the AppDelegate to create this color across the entire app:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor colorWithRed:0.16
green:0.20
blue:0.32
alpha:1];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
[self setBarStyle:UIBarStyleBlack];
[self setTintColor:color];
}
However, in one of my views, i would like to change the color of one of the nav bar items to another color, different from the global color above, but only for one of the items - the bar color should stay the same (reasoning - i'd like to have a nav bar item in green and a "On" text and change it to red with an "Off" text based on the user input).
I tried to override the color of the button in my view in the following way, but it doesnt seem to do anything.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
}
Does anyone have a suggestion (or a better way) to make this happen?
Cheers.
It is tested code which will work 100%.
//in below code u can set the different image for different color
or
simply
fill the color by your question code.
here below is for put image in navigation bar
u can customize by removeing the picture code and puttting ur above code with different color.that it .logic is same
CustomNavigation.h
#import <Foundation/Foundation.h>
#interface UINavigationBar (UINavigationBarCustomDraw){
}
#end
CustomNavigation.m
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.5f
green: 0.5f
blue:0
alpha:1]];
if ([self.topItem.title length] > 0) {
if ([self.topItem.title isEqualToString:#"First"]) {
[[UIImage imageNamed:#"First.png"] drawInRect:rect];
}
else if ([self.topItem.title isEqualToString:#"Second"]) {
[[UIImage imageNamed:#"Second.png"] drawInRect:rect];
}
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.topItem.title;
self.topItem.titleView = label;
}
else {
[[UIImage imageNamed:#"wood.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
if u want to First.png to set navigationBar background image in FirstViewController then
in ur FirstViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"First";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
if u want to Second.png to set navigationBar background image in SecondViewController then
in ur SecondViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"Second";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
I am working on iPhone app. I have added Navigation bar Background image
With interface: -
#interface UINavigationBar (backgroundImageWithTitle)
And method: -
- (void)drawRect:(CGRect)rect
By this method Navigation bar background images is being set one time.
I want to call it from different .m files for assigning different images on bar.
How it can be implemented?
Thanks in advance.
CustomNavigation.h
#import <Foundation/Foundation.h>
#interface UINavigationBar (UINavigationBarCustomDraw){
}
#end
CustomNavigation.m
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.5f
green: 0.5f
blue:0
alpha:1]];
if ([self.topItem.title length] > 0) {
if ([self.topItem.title isEqualToString:#"First"]) {
[[UIImage imageNamed:#"First.png"] drawInRect:rect];
}
else if ([self.topItem.title isEqualToString:#"Second"]) {
[[UIImage imageNamed:#"Second.png"] drawInRect:rect];
}
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.topItem.title;
self.topItem.titleView = label;
}
else {
[[UIImage imageNamed:#"wood.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
if u want to First.png to set navigationBar background image in FirstViewController then
in ur FirstViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"First";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
if u want to Second.png to set navigationBar background image in SecondViewController then
in ur SecondViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"Second";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
On iOS 5 this will not work but the good news is that there is a simple way of doing this
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"name"] forBarMetrics:UIBarMetricsDefault];
NOTE: This will only work on iOS 5 and above so make sure you check iOS version if you want to be backwards compatible.
Or other option so that code runs only incase of iOS 5
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
}
I have tried this
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"name"] forBarMetrics:UIBarMetricsDefault];
It works fine for iOS 5.0 but got crashed in iOS 4.3 or below.
You have either set conditional i.e for
iOS 5
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"name"] forBarMetrics:UIBarMetricsDefault];
else
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"name"]];
but still there is issue that it shows line under image.
Or you can do in this way
self.navigationItem.hidesBackButton = YES;
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"image.png"];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
imgView.image = image;
[tempView addSubview:imgView];
[navBar clearBackgroundImage];
[navBar addSubview:tempView];
[tempView release];
You can try in this way
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
//[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
}
and in else case you can add previous code i.e setBackgroundImage ....
Using (SDK 3.2.5.) - In one of my Apps I needed navigationBar with custom image both in landscape and portrait.
What I did was:
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image;
if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width.
{
image = [UIImage imageNamed: #"portraitNavigationBar.png"];
}
else
{
image = [UIImage imageNamed: #"landscapeNavigationBar.png"];
}
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Luckily my app supports rotation - so, when rotating - navigation bar automatically redraws itself.
But to manually redraw it - you can also call :
[navigationController.navigationBar setNeedsDisplay];
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.