2

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

trungduc
  • 11,926
  • 4
  • 31
  • 55
Seize Duh
  • 61
  • 1
  • 8
  • What is your purpose? And how you will go to HomeViewController, if you are not setting it in app delegate? – Manish Mahajan May 11 '18 at 07:19
  • 1
    You can't add `UINavigationController` inside `viewDidLoad` and `viewWillAppear` but you can add an `UINavigationBar`. Check this post https://stackoverflow.com/questions/21448766/adding-navigation-bar-programmatically-ios – trungduc May 11 '18 at 07:21
  • I updated my question with more information – Seize Duh May 11 '18 at 07:30
  • @SeizeDuh did you try to use `let frontViewController = UINavigationController(rootViewController: homeViewController())` – trungduc May 11 '18 at 07:33
  • @trungduc It works with `let frontViewController = UINavigationController(rootViewController: homeViewController())` – Seize Duh May 11 '18 at 07:40

2 Answers2

4

Replace

let frontViewController = HomeViewController()

with

let frontViewController = UINavigationController(rootViewController: HomeViewController())

and it will work.

trungduc
  • 11,926
  • 4
  • 31
  • 55
1

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
}
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19