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

5
  • What is your purpose? And how you will go to HomeViewController, if you are not setting it in app delegate? Commented May 11, 2018 at 7:19
  • 1
    You can't add UINavigationController inside viewDidLoad and viewWillAppear but you can add an UINavigationBar. Check this post stackoverflow.com/questions/21448766/… Commented May 11, 2018 at 7:21
  • I updated my question with more information Commented May 11, 2018 at 7:30
  • @SeizeDuh did you try to use let frontViewController = UINavigationController(rootViewController: homeViewController()) Commented May 11, 2018 at 7:33
  • @trungduc It works with let frontViewController = UINavigationController(rootViewController: homeViewController()) Commented May 11, 2018 at 7:40

2 Answers 2

4

Replace

let frontViewController = HomeViewController()

with

let frontViewController = UINavigationController(rootViewController: HomeViewController())

and it will work.

Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.