设置UINavigationbar的标题不起作用

问题描述 投票:0回答:2

我查看了几个在线教程,但没有任何工作。

这是我的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中的任何内容。

谢谢你的回答。

swift uikit uinavigationbar uinavigationitem
2个回答
4
投票

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

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

然后在你的ViewController班加

self.title = "AAA"

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

class ViewController: UIViewController {

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

你需要用UINavigationController替换UIViewController


2
投票

从故事板中选择ViewController。

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

enter image description here

1)选择导航项并从故事板中设置标题。

enter image description here

2)通过编程方式

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Your Title"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.