简体   繁体   English

以编程方式将NavigationController添加到指定viewController中,而无需故事板快速

[英]Add navigationController to specif viewController programmatically without storyboard swift

I want to add navigationController to just only for HomeViewController for example. 例如,我只想为HomeViewController添加navigationController I know how to do it from AppDelegate and like this below 我知道如何从AppDelegate做到这一点,如下所示

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 ? 我还有另一种方法可以在viewDidLoadviewWillAppear添加navigationController吗?

Edited: 编辑:

My logic is when I pressed Login button which is the code below. 我的逻辑是当我按下下面的代码登录按钮时。 Then it will present SWRevealViewController 然后将显示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 我只想将navigationController设置为HomeViewController

Look below code hope it works for you... 看下面的代码希望它对您有用...

This will be in App delegate 这将在App委托中

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
}

Replace 更换

let frontViewController = HomeViewController()

with

let frontViewController = UINavigationController(rootViewController: HomeViewController())

and it will work. 它会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式导航到另一个故事板上的navigationcontroller和tabbarcontroller内的viewcontroller - Navigate programmatically to viewcontroller inside navigationcontroller & tabbarcontroller on another storyboard 快速地以编程方式创建NavigationController时,ViewController出现了多次 - ViewController appears mulitple times when creating a navigationController programmatically in swift Swift-TabBarController-> ViewController-> NavigationController - Swift - TabBarController -> ViewController -> NavigationController 以编程方式将UIButton添加到自定义ViewController的情节提要 - Programmatically add UIButton to custom ViewController with storyboard in place Swift 5. 如何在按钮触摸时呈现或显示 ViewController? 没有故事板(以编程方式) - Swift 5. How present or show ViewController on button touch? Without Storyboard (programmatically) 目前ViewController Segue Transition没有故事板[Swift] - Present ViewController Segue Transition Without Storyboard [Swift] 在ViewController中以编程方式使用NavigationController - Programmatically use NavigationController inside ViewController 以编程方式创建一个 navigationController (Swift) - Creating a navigationController programmatically (Swift) 在Storyboard中以编程方式呈现ViewController - presenting ViewController Programmatically in Storyboard 从ViewController(故事板)调用NavigationController(xib) - Call NavigationController (xib) from ViewController (storyboard)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM