1

I got this image that I want to use in my navigation controller:

enter image description here

I'm setting it like this(following advice from this question):

UINavigationBar *theBar = self.navigationController.navigationBar;
if ( [theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"cabecalho.jpg"];
    [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

But when I run my app, I get this:

enter image description here

What do I do so my image can fit the navigation controller?

Thank you.

Community
  • 1
  • 1
gabsferreira
  • 3,089
  • 7
  • 37
  • 61
  • I had the same issue and basically created the image to be the same width as the navbar - is this not an option for you? – Jai Govindani Apr 03 '13 at 04:00
  • It looks that your image dimension is less than 320 the width of navigation bar, if the image is small it will repeat in the background, make sure your image width is 320 px, or, 640 px for Retina.. – iphonic Apr 03 '13 at 04:13
  • This image is 320x49. It should fit, right? – gabsferreira Apr 03 '13 at 04:19
  • 1
    Here is your answer http://stackoverflow.com/questions/1692487/how-to-add-background-image-on-iphone-navigation-bar . The code you are trying will only work on >=iOS 5.0 not below.. you can subclass Navigation bar and draw the image.. – iphonic Apr 03 '13 at 04:29
  • Just make it in 320*44 size. it will fit on navigation bar. otherwise it will not work – Monish Bansal Apr 03 '13 at 04:16

2 Answers2

2

Try with following code

[self.navigationBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]];

Refer How to add background image on iphone Navigation bar? this question that may be helpful for you.

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

I found the solution following the example of this question.

When I create my UINavigationController, I do this:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewLista];

if ([nav.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"cabecalho.png"];
    [nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
gabsferreira
  • 3,089
  • 7
  • 37
  • 61