Creating a Navigationcontroller Programmatically (Swift)

Creating a navigationController programmatically (Swift)

Swift 1, 2:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
}

Swift 4+: and Swift 5+

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
}

How to add a navigation controller programmatically in code but not as initial view controller

You can add UINavigationController like below :

First you have to create object of SecondViewController,

let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")

Or

let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)

Or

let myViewController: SecondViewController? = SecondViewController()

Then add Navigation to SecondViewController

let myNavigationController = UINavigationController(rootViewController: myViewController!)

If you want to present then use :

self.present(myNavigationController, animated: true) { 
}

If you want to push then use :

self.navigationController?.pushViewController(myNavigationController, animated: true)

If you want to set as root controller then use :

let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController

How to embed a view controller into a navigation view controller programmatically

It is done like this:

// example ViewController
let myVC = UIViewController()

// create the NavigationController with my VC as root
let navCon = UINavigationController(rootViewController: myVC)

navigation controller that i create programmatically not appearing as in examples

If you are using Xcode 11 and iOS 13, you need to write same code in SceneDelegate.swift. The main reason for Apple to add UISceneDelegate to iOS 13 was to create a good entry point for multi-windowed applications.

If you do not intend to support multiple windows, you can remove SceneDelegate.swift file remove UIApplicationSceneManifest key from Info.plist and remove func application(_: configurationForConnecting:, options:) and func application(_:, didDiscardSceneSessions:) methods from AppDelegate.swift

Learn more about Scene Delegate on donnywalls - Understanding the iOS 13 Scene Delegate

How to create a UINavigationControll in UIViewController programmatically Swift

Maybe have a look at "Showing and Hiding View Controllers" where some navigation concepts are explained: https://developer.apple.com/documentation/uikit/view_controllers/showing_and_hiding_view_controllers

When using storyboards you should be able to use a segue to navigate from one view controller to another.

iOS: Navigation Bar of a Programmatically created UINavigationController Not Expanding to Safe Area

Just add this to the end of your navigationBarConfiguration func

        if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.backgroundColor = UIColor.systemBlue
controller.navigationBar.standardAppearance = navBarAppearance
controller.navigationBar.scrollEdgeAppearance = navBarAppearance
} else {
controller.edgesForExtendedLayout = []
}

Sample Image

Swift programmatically navigate to another view controller/scene

Swift 5

The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.

To retain the old style you need to modify the view controller you will be presenting like this:

newViewController.modalPresentationStyle = .fullScreen

This is the same for both programmatically created and storyboard created controllers.

Swift 3

With a programmatically created Controller

If you want to navigate to Controller created Programmatically, then do this:

let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)

With a StoryBoard created Controller

If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)

Making a UITabBarController and UINavigationController Programmatically Swift 4

I think there is a typo in your snippet. In the following code, we added an intermediate vc to solve recursive problems.

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

window = UIWindow(frame: UIScreen.main.bounds)
let journalVC = JournalViewController()
let navController = UINavigationController(rootViewController: journalVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()

return true
}

Here is journal vc:

class JournalViewController: UIViewController{

var tabBarCnt = UITabBarController()

override func viewDidLoad() {
super.viewDidLoad()

tabBarCnt = UITabBarController()
tabBarCnt.tabBar.barStyle = .black

let journalVC = JournalTableViewController()
journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)

tabBarCnt.viewControllers = [journalVC]
self.view.addSubview(tabBarCnt.view)

}

}

while the tableviewcontroller should be like this:

class JournalTableViewController: UITableViewController{


}

Add navigationController to specif viewController programmatically without storyboard swift

Replace

let frontViewController = HomeViewController()

with

let frontViewController = UINavigationController(rootViewController: HomeViewController())

and it will work.



Related Topics



Leave a reply



Submit