Search code examples
iosswiftuinavigationcontrolleruinavigationbaruiwindow

Swift -How to access navBar properties on second uiwindow


I have a second window that I create, i add a navVC to it and then the nav's root is blueVC.

Inside blueVC I have some logic that either shows a webView (I hide the navBar) or if necessary it adds another vc (redVC -show navBar) to itself as a child.

The problem is in redVC I add a BarButtonItem to it but it's not appearing. The navVC is present but I can't seem to access any of the navBar's properties either.

Where am I having the problem at?

let secondWindow = SecondWindow() // subClassed from UIWindow

var navVC: UINavigationController?
let blueVC = BlueVC()

func launchSecondWindow() {

    navVC = UINavigationController(rootViewController: blueVC)

    secondWindow.frame = CGRect ...
    secondWindow.rootViewController = navVC!
    secondWindow.backgroundColor = .clear
    secondWindow.windowLevel = UIWindow.Level.normal
    secondWindow.rootViewController = safeNavVC
    secondWindow.makeKeyAndVisible()     

    // doesn't show, the navBar stays gray
    secondWindow.rootViewController?.navigationController?.navigationBar.barTintColor = .purple

    // present it
}

BlueVC:

BlueVC: UIViewController {

let redVC = RedVC()

logic() {

    // some logic that decides to add the webView or the redVC

    if !redVC.view.isDescendant(of: self.view) {

        addChild(redVC)
        redVC.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(redVC.view)
        redVC.didMove(toParent: self)

        redVC.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        redVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        redVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        redVC.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }
}

RedVC:

RedVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // doesn't show
    navigationItem.title = "123"

    // doesn't show
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(...))
}
}

Solution

  • The problem was I added the navigation title and the BarButtonItem to the wrong vc. Since the redVC was a child of the blueVC the blueVC is where I should have added it.

    BlueVC: UIViewController {
    
    let redVC = RedVC()
    
    logic() {
    
        // some logic that decides to add the webView or the redVC
    
        if !redVC.view.isDescendant(of: self.view) {
    
            // ** now it shows shows
            navigationItem.title = "123"
    
            // ** now it shows
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(...))
    
            addChild(redVC)
            redVC.view.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(redVC.view)
            redVC.didMove(toParent: self)
    
            redVC.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
            redVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            redVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            redVC.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        }
    }