Swift - 如何访问第二个 uiwindow 上的 navBar 属性

Swift -How to access navBar properties on second uiwindow

我创建了第二个 window,我向其添加了一个 navVC,然后导航的根是 blueVC。

在 blueVC 内部,我有一些逻辑可以显示一个 webView(我隐藏导航栏),或者如果需要,它会添加另一个 vc(redVC -show navBar)作为子项。

问题出在 redVC 中,我向其中添加了一个 BarButtonItem,但它没有出现。 navVC 存在,但我似乎也无法访问 navBar 的任何属性。

我哪里有问题?

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(...))
}
}

问题是我将导航标题和 BarButtonItem 添加到了错误的 vc。由于 redVC 是 blueVC 的 child,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) {

        // ** 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
    }
}