Set the title of Navigation Bar created in Interfa

2019-01-31 16:21发布

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.

8条回答
冷血范
2楼-- · 2019-01-31 17:01

Here is the Simplest way:

[[[self.navigationController navigationBar] topItem] setTitle:@"The Title"];
查看更多
混吃等死
3楼-- · 2019-01-31 17:08

you can use existing navigation bar with following code

self.navigationController?.navigationBar.topItem?.title = "Calender"
查看更多
▲ chillily
4楼-- · 2019-01-31 17:12

(Using XCode 4.5.2) Found a method that works directly in Interface Builder - without setting in code.

With the ViewController object selected in (IB), under the Identity Inspector there is a section labeled "User Defined Runtime Attributes". I added the keyPath "title" of type String and set it to the value I wanted.

Tested and this works when pushed onto a Nav Controller stack and when opened modally (with a Modal Nav Controller of course).

查看更多
一夜七次
5楼-- · 2019-01-31 17:13

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.

查看更多
女痞
6楼-- · 2019-01-31 17:13

in my .h file

  • Dragged the Child of the navigation item from the storyboard into the .h file

  • Named it "navigationBarTitle"

in my .m file

  • Added self.navigationBarTitle.title = @"Some Title";
查看更多
甜甜的少女心
7楼-- · 2019-01-31 17:13

Simply put your UIViewController into a new UINavigationControllers view controller stack and present that navigation controller modally.

查看更多
登录 后发表回答