【问题标题】:Setting title of UINavigationbar not working设置 UINavigationbar 的标题不起作用
【发布时间】:2019-04-11 11:06:20
【问题描述】:

我浏览了一些在线教程,但没有任何效果。

这是我的 viewController 的代码:

import UIKit

class ViewController: UINavigationController {

    let textView = UITextView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // tried this
        self.navigationItem.title = "AAA"

        // and this
        self.title = "AAA"

        // and finally this
        self.parent?.title = "AAA"
    }
}

我不明白为什么这不起作用(我以前没有使用过导航栏)

我没有更改 main.storyboard 中的任何内容。

感谢您的回答。

【问题讨论】:

  • 你确定你的 ViewController 嵌入了 NavigationController 吗?你的视图控制器类将是类 ViewController: UIViewController { }
  • 那么如何添加导航栏。如果我使用 "class ViewController: UIViewController { } " 我不能使用 "self.navigationBar...."

标签: swift uikit uinavigationbar uinavigationitem


【解决方案1】:

首先在你的故事板中选择你的视图控制器,然后

编辑器 -> 嵌入 -> 导航控制器

然后在你的ViewController 类中添加

self.title = "AAA"

在您的 viewDidLoad 方法中,您的代码将如下所示:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "AAA"
    }
}

您需要将UINavigationController 替换为UIViewController

【讨论】:

    【解决方案2】:

    从情节提要中选择 ViewController

    转到编辑器并嵌入导航控制器

    1) 选择导航项并从情节提要中设置标题。

    2) 以编程方式

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = "Your Title"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多