Seize Duh
Seize Duh

Reputation: 61

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

Upvotes: 2

Views: 5386

Answers (2)

trungduc
trungduc

Reputation: 12144

Replace

let frontViewController = HomeViewController()

with

let frontViewController = UINavigationController(rootViewController: HomeViewController())

and it will work.

Upvotes: 4

Manish Mahajan
Manish Mahajan

Reputation: 2082

Look below code hope it works for you...

This will be in App delegate

if UserDefaults.standard.bool(forKey: REMEMBER_ME) {
   let menuVC = UINavigationController(rootViewController: SideMenuViewController())
   let loginVC = UINavigationController(rootViewController: DashboardViewController())
   let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
   self.navigationController?.navigationBar.isHidden = true
   window?.rootViewController = revealViewController
} else {
   window?.rootViewController = LoginViewController()
}

This will be in your login action

if let window = UIApplication.shared.keyWindow {
    let menuVC = UINavigationController(rootViewController: SideMenuViewController())
    let loginVC = UINavigationController(rootViewController: DashboardViewController())
    let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
    self.navigationController?.navigationBar.isHidden = true
    window.rootViewController = revealViewController
}

Upvotes: 1

Related Questions