Setting title of UINavigationbar not working

孤街浪徒 提交于 2019-12-02 14:19:27

问题


I've looked through a few online tutorials, but nothing is working.

That's the code of my 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"
    }
}

I don't understand why this isn't working (I haven't used a navigation bar before)

I didn't change anything in the main.storyboard.

Thanks for your answers.


回答1:


First of all in your storyboard select your view controller and then

Editor -> Embed In -> Navigation Controller

then in your ViewController class add

self.title = "AAA"

in your viewDidLoad method and your code will look like:

class ViewController: UIViewController {

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

You need to replace UINavigationController with UIViewController




回答2:


Select ViewController from the storyboard.

Go to the Editor and Embed with Navigation Controller

1) Select Navigation Item and set title from the storyboard.

2) By Programmatically

class ViewController: UIViewController {

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


来源:https://stackoverflow.com/questions/55631031/setting-title-of-uinavigationbar-not-working

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