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

标签 ios swift uinavigationcontroller uinavigationbar uiwindow

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

在 blueVC 内部,我有一些逻辑可以显示一个 webView(我隐藏了 navBar),或者如果有必要,它将另一个 vc(redVC -show navBar)添加到自己作为 child 。

问题出在 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: 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
    }
}

红色VC:

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 的子项,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
    }
}

关于ios - Swift - 如何访问第二个 uiwindow 上的 navBar 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56743213/

相关文章:

ios - 如何定义嵌入 ViewController 的 NavigationController?

ios - 使用 popToViewController 返回到黑屏的特定 View Controller 时出现问题

ios - 拖到 viewcontroller 的导出不起作用

ios - 如何在 Swift 中转换 NSData 和 NSString 表示之间的推送 token ?

ios - 核心数据谓词对多

swift - AppDelegate中applicationDidEnterBackground时调用ViewController

ios - 在加载时设置 UINavigationController 选项卡栏项目

ios - Firebase 方法不起作用

ios - 我无法使用此代码修复 AVcapture 的大小

ios - Swift 4 - Collection View 将节插入应用于标题?