Set the title of Navigation Bar created in Interface Builder

让人想犯罪 __ 提交于 2019-11-29 22:48:23
codelark

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.

Also found this easier way if you've already defined a UINavigationBar in your IB. Assuming self.navigationBar is your IBOutlet:

self.navigationBar.topItem.title = title;

(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).

viewcontroller.h file in create iboutlet of the navigation item

     @property (weak, nonatomic) IBOutlet UINavigationItem *navigationBarTitle;

- Named it "navigationBarTitle"

in viewcontroller.m file in add the code in super viewDidLoad

      self.navigationBarTitle.title = @"Favorites";

connect the storybord in navigation bar item to navigationBarTitle.

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";

Here is the Simplest way:

[[[self.navigationController navigationBar] topItem] setTitle:@"The Title"];

you can use existing navigation bar with following code

self.navigationController?.navigationBar.topItem?.title = "Calender"

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!