【问题标题】:How to set the title of a Navigation Bar programmatically?如何以编程方式设置导航栏的标题?
【发布时间】:2011-09-03 11:56:57
【问题描述】:

我在视图中有一个导航栏,我想以编程方式更改其导航项标题。

这是怎么做到的?

【问题讨论】:

    标签: ios iphone view uinavigationbar


    【解决方案1】:

    在你的 UIViewController 的 viewDidLoad 中

    self.navigationItem.title = "The title";  //In swift 4
    

    你可以

    self.title = "The title"
    

    会成功的

    你也可以把上面的语句放在app delegate中进行全局复制。

    【讨论】:

      【解决方案2】:

      你也可以用??

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

      【讨论】:

      • 你可以,但你为什么要这样做?视图控制器不知道它在顶部,所以这似乎可能会引起混淆。 self.title 更精确。
      【解决方案3】:

      此代码对我不起作用

      self.navigationItem.title = @"Title here";
      

      但这行得通。

      self.title = "Title Name"
      

      【讨论】:

        【解决方案4】:

        Swift 3 版本:

        如果你使用没有导航控制器的导航栏,那么你可以试试这个:

        navigationBar.topItem?.title = "Your Title"
        

        希望得到帮助。

        【讨论】:

          【解决方案5】:

          有时您可能会遇到更新标题但没有产生任何影响的情况,这可能是当您在 topItem 上有自定义视图时,因此您可能希望像这样更新它:(self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"

          【讨论】:

            【解决方案6】:

            非常重要的注意事项:确保在父视图控制器的 viewDidLoad 方法或情节提要中的父导航栏中进行上述更改

            【讨论】:

              【解决方案7】:

              来自Apple Documentation

              使用导航栏最常见的方式是使用导航 控制器。您还可以将导航栏用作独立对象 在您的应用中。

              所以如果你有一个 UINavigationController,那么你需要做的就是设置导航栏的标题(如所有之前的答案中所述)

              self.navigationItem.title = @"title";
              

              但是如果你有一个独立的导航栏,它是在 UIViewController 的对象中以编程方式创建的,你必须通过创建适当的 UINavigationItem 对象并将它们添加到导航栏对象堆栈来设置导航栏的初始外观,即

              1. 创建导航栏实例(UINavigationBar)
              2. 创建导航栏项 (UINavigationItem) 的实例,该实例管理要在 UINavigationBar 对象中显示的按钮和视图。请注意,您在这一步设置了标题。
              3. (可选步骤)向导航栏项添加右/左按钮(UIBarButtonItem 的实例)。

              上述步骤的 Objective-C 代码示例:

              UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
              
              /* Create navigation item object & set the title of navigation bar. */
              UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name];
              
              /* Create left button item. */
              UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
              navItem.leftBarButtonItem = cancelBtn;
              
              /* Create left button item. */
              UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
              navItem.rightBarButtonItem = doneBtn;
              
              /* Assign the navigation item to the navigation bar.*/
              [navbar setItems:@[navItem]];
              
              /* add navigation bar to the root view.*/
              [self.view addSubview:navbar];
              

              对于独立导航栏的 Swift 版本,请查看this answer

              【讨论】:

                【解决方案8】:

                在 ViewDidLoad 方法中使用它

                格式:

                @property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
                

                示例:

                self.navigationItem.title = @"Title here";
                

                【讨论】:

                  【解决方案9】:

                  在 viewDidLoad 中

                    self.title = @"Title";
                  

                  【讨论】:

                  • 我的两分钱 :) 这可行,尽管如果 vewcontroller 是工具栏控制器的一部分。然后工具栏项的标题也会更新。
                  【解决方案10】:

                  将下面的代码写在viewDidLoad或者可能更好的是initWithNibName:

                    self.navigationItem.title = @"title";
                  

                  谢谢。

                  【讨论】:

                    【解决方案11】:

                    在你的 UIViewController 中

                    self.navigationItem.title = @"The title";
                    

                    【讨论】:

                    • 是的,我会选择这个,因为它只更新navigationItem的标题,而不是toolbarItem的标题。
                    猜你喜欢
                    • 1970-01-01
                    • 2012-11-09
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-12-24
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多