1

I want to insert an image to the navigation bar.

My code is

 UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"bodyBg.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

I have attached the screenshot of the current output. Is this because of the large image size?

enter image description here

But my desired output is enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mano
  • 670
  • 1
  • 9
  • 28
  • 2
    Well yes .... You should reduce the size – IronManGill Aug 05 '13 at 11:53
  • No it should not be the reason for this. – Nishant Tyagi Aug 05 '13 at 11:56
  • I am sure it's bcoz of image height. You should really reduce the size. Or else as others said you need to use that image as pattern image. – Dinesh Raja Aug 05 '13 at 12:10
  • Check this link you can get your answer here [link][1] [1]: http://stackoverflow.com/questions/7764309/changing-the-uinavigationbar-background-image – iPhone Programmatically Aug 05 '13 at 12:16
  • 1
    Can you upload your navigation bar image on [imgur](http://imgur.com) so we can see what might be a problem. Also I described you in [this answer](http://stackoverflow.com/questions/18057493/how-to-apply-a-background-image-to-navigation-bar/18057826#18057826) what is the easiest way to implement image to UINavigationBar. – TCvetkovic Aug 05 '13 at 12:36

8 Answers8

1

First, make your navigation bar image size 1024x44 pixels nad for retina display 2048x88 pixels.

If you have the same image for UINavigationBar on every view controller, put this to AppDelegate in method didFinishLaunchingWithOptions:

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav-background.png"] forBarMetrics:UIBarMetricsDefault];
// This will remove shadow in iOS6
        if ([[UINavigationBar class] instancesRespondToSelector:@selector(shadowImage)]) {
            [[UINavigationBar appearance] setShadowImage:[[[UIImage alloc] init] autorelease]];
        }

And also I see you need custom back button, also put this in AppDelegate:

    UIImage *backButtonNormal = [UIImage imageNamed:@"nav-back.png"];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
TCvetkovic
  • 118
  • 1
  • 8
  • In that second image, there are those bar button items right.. how to set those buttons in desired position? – Mano Aug 05 '13 at 13:16
0
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];

I hope , it will work for u

Kanhaiya Sharma
  • 1,040
  • 8
  • 20
0

Use UINavigationBar's setBackgroundImage: forBarMetrics: method :

[self.navigationController.navigationBar setBackgroundImage:yourImageHere forBarMetrics:UIBarMetricsDefault];

your UIImage should be of appropriate height and width. For example iphone 4 retina size would be 640*88 for portrait.

EDIT : Point here is if UIImage height is bigger say 150 pixel height it will display that much height and our UINavigationBar height is 44 pixel.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

you can set the color of navigation bar same as your image use below code and you can put image name which you want to show in navigation bar

self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bodyBg.png"]];
Pratik
  • 2,399
  • 17
  • 36
0

try it out with this code..

if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    [navBar setBackgroundImage:[UIImage imageNamed:@"Nav.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
    UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
    if (imageView == nil)
    {
        imageView = [[UIImageView alloc] initWithImage:
                    [UIImage imageNamed:@"Nav.png"]];
        [navBar insertSubview:imageView atIndex:0];
        [imageView release];
    }
}

see my full answer from this link setting-title-for-uinavigation-bar-in-iphone

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • in that second image, there are those bar button items right.. how to set those buttons in desired position? – Mano Aug 05 '13 at 13:10
0

check this link for add background image in navigationbar and first you your navigationbar image size set as navigationbar.

check this link here

i hope this code useful for you.

Community
  • 1
  • 1
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
0
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] 
           forBarMetrics:UIBarMetricsDefault];

and also this link

Community
  • 1
  • 1
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
0

Try this code it works for me:

UIView* navigationBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImage * targetImage = [UIImage imageNamed:@"Header_top.png"];
UIGraphicsBeginImageContextWithOptions(navigationBGView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, navigationBGView.frame.size.width, navigationBGView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
navigationBGView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
[self.navigationController.navigationBar addSubview:navigationBGView];
[self.navigationController.navigationBar setTintColor:[UIColor clearColor]];
Vishnu
  • 354
  • 3
  • 19