Search code examples
swiftuiviewcontrolleruinavigationitem

navigationItem not displayed when VC called with self.present


need an help on this as I am struggling for few days now...

I have a VC with a Navigation Item called from viewDiDLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    self.customization()
    self.fetchData()
}

...

func customization() {
        self.navigationItem.title = self.currentUser?.displayName
    self.navigationItem.setHidesBackButton(true, animated: false)
    let icon = UIImage.init(named: "back")?.withRenderingMode(.alwaysTemplate)
    let backButton = UIBarButtonItem.init(image: icon!, style: .plain, target: self, action: #selector(self.dismissSelf))
    backButton.tintColor = UIColor(red: 0.0157, green: 0.498, blue: 0, alpha: 1.0)
    self.navigationItem.leftBarButtonItem = backButton
    self.locationManager.delegate = self
    print("called customization()")
}

This works flawlessy with no issues when called with triggered segue Show.

When I call the VC with the below :

    let storyBoard: UIStoryboard = UIStoryboard(name: "Storyboard", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
self.present(viewController, animated: true, completion: nil)

I got the method to reach viewDidLoad and customizations(), from the console:

called customization() 

but navigationItem is not displaying.

Tried to copy the methods in customization() from the calling method, they are executed but no show as well.

Any idea what's wrong here please?


Solution

  • You have to add navigation when presenting:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Storyboard", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
    let nav = UINavigationController(rootViewController: viewController)
    self.present(nav, animated: true, completion: nil)