Search code examples
iosswiftuiviewcontrolleruinavigationbaruinavigationitem

(Swift) Navigation bar is hidden in viewController


I want to present SecondVC from FirstVC and make SecondVC have a rightBarButtonItem called Close which calls an @objc function which dismisses SecondVC.
Also, I want to change secondVC's title from firstVC:
This is how I present SecondVC from the FirstVC:

let secondVC = AdviceDetailsViewController()
secondVC.modalPresentationStyle = .fullScreen
secondVC.title = "Example" //Value of type 'UINavigationController' has no member 'myTitle'
self.present(secondVC, animated: true)

Code for navigationBar in secondVC:

public var myTitle: String = ""
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .gray
    self.title = myTitle
    self.navigationController?.navigationBar.barTintColor = UIColor.orange
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(closeDetails))
}

@objc func closeDetails() {
    self.dismiss(animated: true, completion: nil)
}

No Navigation bar is visible in secondVC, the only visible thing is gray background color.
What should I change? I am doing everything programmatically in this app.


Solution

  • Use this method to show second VC

    let nc = UINavigationController(rootViewController: AdviceDetailsViewController())
    nc.modalPresentationStyle = . fullScreen
    self.present(nc, animated: true)
    

    Result