iOS 5 – Problem Setting an Image for UINavigationBar Background

iOS 5 has made it much, MUCH easier to set an image as the background of your UINavigationBar. There are dozens of tutorials out there that will get you there, and I tried a half dozen of them, but still couldn’t get it to work.

The code itself is very simple, and here it is:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"pw-bkg.png"] forBarMetrics:UIBarMetricsDefault];

The magic is in the UIAppearance protocol.

The Problem

The problem was that I didn’t know where to put it. Some of the articles recommended in the UIViewController viewWillAppear or viewDidLoad methods, but that wasn’t working for me when using my initial VC.

The Answer

I’m not saying that your view controller isn’t the right place to do it, but it wasn’t working for me. Ultimately, I put it in the AppDelegate (UIApplicationDelegate) application didFinishLaunchingWithOptions method, and it works like a charm.

Note! – this will break iOS 4.x apps, so make sure you handle that contingency if you’re supporting iOS 4 and 5.

If you got stuck like I did, hope this helps!

Byron

, , ,

One Response to iOS 5 – Problem Setting an Image for UINavigationBar Background

  1. link here December 28, 2011 at 7:14 pm #

    this was a great portion thanks for the help.

Powered by WordPress. Designed by Woo Themes