Stephane
Stephane

Reputation: 5076

Customizing the background image for a navigation bar created programmatically

I'm stuck at customizing the navigation bar of my app. I added the navigation controller programmatically. Indeed, in the method application:didFinishLanchingWithOptions of my app delegate, I've added and hidden a navigation bar using the following code:

self.window.rootViewController  = self.viewController;
// ListingNav previously declared as instance variable
self.ListingNav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.ListingNav.navigationBarHidden = YES;
[self.window addSubView:ListingNav.view];
[self.window makeKeyAndVisible];
return YES;

Now, I'd like to set an image as a navigation bar background, set another background image for the "back" button and add another button on the right side of the same navigation bar with its own background image too. Any idea on how to proceed ?

Thx for helping,

Stephane

Upvotes: 0

Views: 2804

Answers (4)

Jose Olmos Martinez
Jose Olmos Martinez

Reputation: 21

You can try:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"ImageNameWithOutExtension"] forBarMetrics: UIBarMetricsDefault];

Upvotes: 2

Wolverine
Wolverine

Reputation: 4329

Use This Code.....

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

Upvotes: 0

Sami
Sami

Reputation: 1389

You can customize the navigation bar without hiding it. Just add these components using addSubview method.

The background image can be added as follows:

UIImageView *imageview = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"bg.png"]];
[self.navigationController.navigationBar addSubview:imageview];

[self.navigationController.navigationBar sendSubviewToBack:imageview];
[imageview release];

Since you need a custom button, you can add UIButton in the same way. Create a new UIButton with its own background and add it.

Upvotes: 1

AliSoftware
AliSoftware

Reputation: 32681

Subclass UINavigationBar and override its drawRect method to draw an image.

If your UINavigationController is then created thru InterfaceBuilder, you can easily change the class of the associated UINavigationBar. If you create your UINavigationController thru code, you can use object_setClass. (this one for example)

There is a lot of questions on StackOverflow already on the exact same subject, don't hesitate to search for "NavigationController background" on the site.

Upvotes: 1

Related Questions