NaviagtionBar item doesn't appear

Hi, I added a navigationItem to my navigation bar but unfortunately it doesn't appear in the simulator.

import UIKit

class ViewController: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

// there are more view controllers 

  let vc2 = SecondViewController()

            self.setViewControllers([vc2], animated: false)

            guard let items = self.tabBar.items else {

                return

            }

            let images = ["house", "bell", "person.circle","person.circle"]

          
            for x in 0..<items.count {

                //items[x].badgeValue= "1"

                items[x].image = UIImage(systemName: images[x])

            }
            self.tabBar.tintColor = .black
           
        }

    }

the relevant code of the SecondViewController

class SecondViewController: UINavigationController {

 override func viewDidLoad() {

        title = "Decount timers"
        super.viewDidLoad()
      view.backgroundColor = .systemBackground
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:  #selector(didTapAddButton))

 
    }

}

Thank you for your help

Replies

You should do it in a content view controller of the UINavigationController.

SecondViewController.swift

import UIKit

class SecondViewController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        self.viewControllers = [RootViewController()]
    }
}

RootViewController.swift

import UIKit

class RootViewController: UIViewController {
    
    override func viewDidLoad() {
        title = "Decount timers"
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:  #selector(didTapAddButton))
    }

    @objc func didTapAddButton(_ sender: Any) {
        //...
    }
}