Search code examples
iosswiftuinavigationcontroller

Add navigationController to specif viewController programmatically without storyboard swift


I want to add navigationController to just only for HomeViewController for example. I know how to do it from AppDelegate and like this below

let navBar = UINavigationController(rootViewController: homeViewController())
self.present(navBar, animated: true, completion: nil)

Is there another way that I can add navigationController inside viewDidLoad and viewWillAppear?

Edited:

My logic is when I pressed Login button which is the code below. Then it will present SWRevealViewController

@IBAction func loginPressed(_ sender: Any) {    
    let frontViewController = HomeViewController()
    let rearViewController  = TableViewController()
    let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController)
    swRevealVC?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
    swRevealVC?.toggleAnimationDuration = 0.30

    self.present(swRevealVC!, animated: true, completion: nil)
}

I just only want to set navigationController to HomeViewController


Solution

  • Replace

    let frontViewController = HomeViewController()
    

    with

    let frontViewController = UINavigationController(rootViewController: HomeViewController())
    

    and it will work.