RootView navigation in swift - ios

I am an Objectie-C developer. for the root view navigation i used the below code in Objectie-c
FirstViewController *fvc=[[FirstViewController alloc]init];
UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:fvc];
self.window.rootViewController=nv;
for my new project I am making it in swift.I just want to make RootView navigation from AppDelegate.swift.

Check below code. first we create window. then alloc init viewcontroller. and then alloc navigation controller with rootcontroller as viewcontroller. and Window's root controller as navigation controller.
var window: UIWindow?
var navC : UINavigationController?
var vc:ViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// alloc init window
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// view controller
self.vc = ViewController(nibName: "ViewController", bundle: nil);
// create navigation controller with root = vc.
self.navC = UINavigationController(rootViewController: self.vc!);
self.navC?.navigationBar.hidden = true;
// window's root controller as navigation controller.
self.window?.rootViewController = self.navC
self.window?.makeKeyAndVisible()
return true
}
Maybe this will help you.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootVie: FirstViewController = FirstViewController() // this is allocation method in swift
if let window = self.window{
window.rootViewController = rootVie
}
return true
}
need referene use this link

Related

Navigation bar is not displayed in Swift 5

I've been working on an iOS app and I'm trying to display a navigation bar to my app. I use a storyboard to just work on some basic UI, but for the other part like the navigation bar, I'm trying to implement it by code.
In the AppDelegate.swift file, I put the following code.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
UINavigationBar.appearance().tintColor = .black
window?.rootViewController = FirstViewController()
window?.rootViewController?.view.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
return true
}
However, the navigation bar doesn't appear on FistViewController.
In the FirstViewController, I also put the following code into the viewwillappear function to display the navigation bar.
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.barStyle = .black
As I said, I have a storyboard, but I only use it for setting FirstViewController as isInitialView controller. I also have a table view on the FirstViewController and I can see it, but I don't see the navigation bar.
So I was wondering if I make a mistake in writing code to display the navigation bar...
Does anyone know what I'm missing in here?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = FirstViewController()
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
let viewController = FirstViewController()
let navVc = UINavigationController(rootViewController: viewController)
window?.rootViewController = navVc
You can not see your navigation bar because you are not embedding your current viewcontroller in to a navigation controller.
Since you are using storyboard you should embed your ViewController in a Navigation controller. You can do that by,
first opening your Main.storyboard file
Select your initial ViewController
Going into Editor > Embed in > Navigation Controller
or you could do it programmatically by refering #jignesh's answer

Can't push view controller in AppDelegate with TabBarController

I have a TabBarController as a rootViewController for my app. And I'm trying to push view controller when user clicks to notification. But code isn't working. How can I push view controller from AppDelegate without storyboards.
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = TabBarController()
window?.makeKeyAndVisible()
return true
}
You can try to embed the tab inside a navigation
let nav = UINavigationController(rootViewController: TabBarController())
nav.isNavigationBarHidden = true
window?.rootViewController = nav
Then inside didReceiveRemoteNotification
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(////
}
to show nav inside the vc viewDidLoad
self.navigationController?.isNavigationBarHidden = false
You should not embed UITabBarController in a UINavigationController (as written in Apple Documentation init and push). However, it's working.
The correct solution is to use UINavigationController as tabs in UITabBarController:
let tabBarController = TabBarController()
tabBarController.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2)]
window?.rootViewController = tabBarController
and then push to them:
let navigationController = tabBarController.selectedViewController as? UINavigationController
navigationController?.pushViewController(notificationViewController)
Or you can create a new view controller and settng it as a rootViewController of window:
window?.rootViewController = notificationViewController
But this requires more navigation code to setting back tabBarController after dismissing notification etc.

Making a UITabBarController and UINavigationController Programmatically Swift 4

I am trying to make a UITabBarController and a UINavigationController programmatically. I've tried many tutorials but most use Swift 3 which is too outdated and doesn't work.
AppDelegate.swift Snippet:
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 = JournalTableViewController()
let navController = UINavigationController(rootViewController: journalVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
JournalTableViewController.swift Snippet:
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)
}
What works:
The build loads onto the simulator
What doesn't work:
The build crashes once loading onto the simulator
After crashing, the error Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee6c2ada8) apears over this line 12 of JournalTableViewController.swift Snippet
If you are going to combine a UITabBarController and a UINavigationController then you will want the tab bar controller to be the root view controller. Each tab can have its own navigation controller if required.
If you make the navigation controller the root, then as soon as you push a new view controller, the tab bar will disappear. By making the tab bar the root, you can have a series of navigation hierarchies and switch quickly between them using the tab buttons.
AppDelegate.swift
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)
let tabBarController = UITabBarController()
navController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
tabBarController.viewControllers = [navController]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
There is no need for any specific code in your view controller class.
JournalTableViewController.swift Snippet:
override func viewDidLoad() {
super.viewDidLoad()
}
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{
}

How to instantiate a viewcontroller with an embedded navigation controller programmatically swift?

I'm trying to instantiate a viewcontroller that has a navigation controller embedded in it from the AppDelegate. Here's what my code looks like:
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let dm = DefaultsManager.sharedManager
if dm.rememberMe == true {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("VolunteerVC")
self.window!.rootViewController = vc
self.window?.makeKeyAndVisible()
}
the right viewcontroller is instantiated but the navigation controller that was embedded in it is now missing. Any idea how to instantiate it with the nav controller still connected?
Figured it out, give the nav controller a name under storyboard ID in the storyboard and for the line:
let vc = storyboard.instantiateViewControllerWithIdentifier("VolunteerVC")
replace the name of the viewcontroller with the name you gave the navigation controller.

Add SubView over TabBar and their Controllers

i`m trying to add a View over the all TabBarController, I tried this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: CGRectMake(0, 0,
CGRectGetWidth(UIScreen.mainScreen().bounds),
CGRectGetHeight(UIScreen.mainScreen().bounds)))
self.window!.backgroundColor = UIColor.whiteColor()
let tabBar : UITabBarController = UITabBarControllerHelper.generate() //Create the TabBarController, works fine
let videoController : UIViewController = VideoViewController()
videoController.view.layer.zPosition = 1
tabBar.view.addSubview(videoController.view)
self.window!.rootViewController = tabBar
self.window!.makeKeyAndVisible()
return true
}
But this don`t work, only TabBarController is rendered. I changed the self.window!.rootViewController to test to draw the videoController and works fine too. The problem is: I can`t merge the views.

Resources