Search code examples
iosiphoneswiftuinavigationbaruinavigationitem

Setting navigation item title issue.. why my navigation item seems many?


I want to change the title of navigation bar but it seems somewhat confusing.. I did self.navigationItem.title = "ipsum" or self.navigationController?.navigationBar.topItem?.title = "ipsum" and so on.. but it fails..

It only succeeds changing the title when trying self.title = "ipsum" but it is not what I want because it causes tabBarItem's title to change too.

I tried debugging and found out strange result.

enter image description here

as you see the picture above.. there seems to be more than two navigationBar so when self.navigationItem.title = "ipsum" it actually changes some other or invisible navigationItem's title so that I can see from the log.

But the UI stays "lorem". Tell me if you figure something out! What am I missing?

EDIT 1:

Main.storyboard enter image description here

Menu.storyboard enter image description here

I put NavigationController in TabBarController and refactored NavigationController to another storyboard reference.

self.navigationItem.title = "ipsum" is called in the viewDidLoad() function.


Solution

  • Now View controllers are:

    ViewController1 attached to item1:

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

    ViewController2 attached to item2:

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

    enter image description here

    Result:

    enter image description here

    enter image description here