1

I use this code to set the background. It do change the background, but there is some problem with Retina.

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navi_bar.png"] forBarMetrics:UIBarMetricsDefault];

I have two pngs: navi_bar.png (320*44) and navi_bar@2x.png (640*88)

The background of navigation bar always uses image navi_bar.png, even in Retina.

If I use this code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navi_bar@2x.png"] forBarMetrics:UIBarMetricsDefault];

The result is this in Retina: enter image description here

The background has the double height...

I have spent the whole morning on this problem. Can anybody help me? Thanks!

理想评论学派
  • 1,001
  • 4
  • 12
  • 21

5 Answers5

3

Try this

  UINavigationBar *navBarName = [[self navigationController] navigationBar];
    UIImage *backgroundImg = [UIImage imageNamed:@"navi_bar"];
    [navBarName setBackgroundImage:backgroundImg forBarMetrics:UIBarMetricsDefault];
Deepesh
  • 8,065
  • 3
  • 28
  • 45
  • Yes, I use your code to change the background in every viewcontroller. It works. But I have to write this code everywhere... Why does my code not work? Is it a bug of apple? – 理想评论学派 Mar 28 '12 at 05:07
  • see this link http://stackoverflow.com/questions/7855365/ios-5-uinavigationbar-appearance-setbackgroundimage-does-not-do-work and check answer – Deepesh Mar 28 '12 at 05:18
  • The problem of the link is not the same with mine. My code can change the background, but the problem is that with Retina, the background still use navi_bar.png, not navi_bar@2x.png. I test your code, It can use the proper png in Retina. – 理想评论学派 Mar 28 '12 at 05:40
  • see this link http://www.mladjanantic.com/setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too/ read when are you use [UINavigationBar appearance] – Deepesh Mar 28 '12 at 06:03
1

I had the same issue: NavBarBg.png is applied for retina device even though NavBarBg@2x.png is available. But it seems to be related with simulator only. When I run the app on device NavBarBg@2x.png is applied correctly.

1

You don't have to write @"navi_bar@2x.png". iOS will automatically pick up the right image for retina display. Just use @"navi_bar.png".

EDIT- This SO question will help

Community
  • 1
  • 1
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
0

You can use: [[UINavigationBar appearance] setBackgroundImage:aImg forBarMetrics:UIBarMetricsDefault];

in iOS 5

Maulik
  • 19,348
  • 14
  • 82
  • 137
0

This is weird you should check whether you have added the 2x image properly or not

Dushyant Singh
  • 721
  • 4
  • 13