Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the title of Navigation Bar created in Interface Builder

I have a modal view controller whose view comes from a XIB. I want it to have the same look and feel as the rest of my navigation based app, but it's not a view that's pushed to the navigation stack, it's presented modally. I dragged a UINavigationBar in and made it an outlet, but it doesn't have a title property to set. I have two fields that can bring up this modal view, and I want the title set differently depending on which one creates the modal view.

like image 967
Steve Avatar asked Jan 12 '11 16:01

Steve


People also ask

How do you set a navigation item title?

Discussion. Use navigationBarTitle(_:) to set the title of the navigation bar. This modifier only takes effect when this view is inside of and visible within a NavigationView .

How do I create a navigation bar in Swift?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.

What is navigation bar in Swift?

A navigation bar manages a stack of UINavigationItem objects. Although the stack is there mostly to support navigation controllers, you can use it to implement your own custom navigation interface. The topmost item in the stack represents the navigation item whose contents are currently displayed by the navigation bar.

What is a navigation bar in IOS?

A navigation bar appears at the top of an app screen, enabling navigation through a hierarchy of content. A navigation bar also provides a natural place to display a screen's title — helping people orient themselves in your app or game — and it can include controls that affect the screen's content.


1 Answers

UINavigationBar's manage a stack of UINavigationItems much like a UINavigationController manager a stack of UIViewControllers. To set what is visible directly, you should use either pushNavigationItem:animated: or setItems:animated: using the navigationItem of the view controller you want the bar to reflect.

eg:

self.navigationItem.title = @"A custom title"; [self.navigationBar pushNavigationItem:self.navigationItem animated:NO]; 

The above code where you have a property navigationBar which references the stand-alone navigation bar.

If you don't want to manage it yourself, you can do as mplappert suggested and nest your view controller (without a stand-alone UINavigationBar) in a UINavigationController and present the navigation controller modally instead of your view controller.

like image 152
codelark Avatar answered Oct 25 '22 01:10

codelark