Tony
Tony

Reputation: 10208

Use custom Navigation Bar in iOS

I added a Navigation Bar to the .xib. I did that because I want to customize a lot of things of it. I want my navigation controller to use that navigation bar in that screen.

I created the outlet named navBar and did:

[self.navigationController.navigationBar = navBar;

But it says that navigationBar is readonly. Is it possible to link my existing navigation controller with the navigation bar that I added to the screen?

Upvotes: 1

Views: 3914

Answers (3)

Judioo
Judioo

Reputation: 1103

It sounds to me that you may not actually want to use a UINavigationBar. As it states in the reference documentation:

The UINavigationBar class implements a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating up and down a hierarchy. The primary properties are a left (back) button, a center title, and an optional right button. You can specify custom views for each of these.

So if you're planned customizations go beyond adding buttons, changing it's color / background, opacity, hiding etc.. you might be better off creating a UIView that mimics the look & feel of a navigation bar.

Here's an example of how to give your UIView that gradient look of a navigation bar.

It's far more flexible and actually quite easy to do BUT you've got alot of reading and testing ahead of you :).

Just in case if it's just buttons you're thinking of adding you might be better off using UIToolbar instead

Upvotes: 1

James Paolantonio
James Paolantonio

Reputation: 2194

You would not manage a navigation bar in interface builder. Also, you would not try to set the navigationBar property of the navigation controller.

To make changes you would make changes to the navigationItem property of the navigationBar

[self.navigationController.navigationBar setItems:newNavItems];

You can also make other changes to the navigationBar such as setting a background image or making it translucent

self.navigationController.navigationBar.translucent = true;
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:barMetrics];

Upvotes: 0

Bruno Koga
Bruno Koga

Reputation: 3874

You can't do that. Since the navigation controller's navigation bar has a lot of settings embedded, it's a readonly property and you shouldn't be able to change it.

What exactly are you trying to achieve that you need to do that via interface builder. I mean, with the newest APIs, it should be simple to do everything you want with a few lines of code, by customising the original navcontroller's navbar.

Upvotes: 0

Related Questions