Navigation bar is not displayed in Swift 5 - ios

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

Related

Show viewcontroller by storyboard id inside UITabBarController

I am trying to show a specific viewcontroller, inside my UITabBarController by using the viewcontrollers storyboard id.
I can only get the viewcontroller to be shown without the UITabBarController.
How do I also show the UITabBarController?
I have tried this:
let identifier = "stats_view"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: identifier)
window?.rootViewController = vc
Which gave me the problem listed above.
try the following code.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let myTabBar = self.window.rootViewController as? UITabBarController { // Getting Tab Bar
myTabBar.selectedIndex = 2 //Selecting tab here
return true
}
}
You should set selectedIndex of tabBarController to view specific viewController which are embedded in tabBarController. Use following code.
self.tabBarController?.selectedIndex = 1

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.

iOS - Setting 'rootViewController' results in black screen on device only

With a very basic single view application, I've deleted the main storyboard file and removed any references to it. As such I'm setting the window rootViewController programmatically. However, while this displays the single view (containing a label) correctly in simulator, it displays a black screen when running it on device. Here is the only code for the app.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = DummyViewController()
window?.makeKeyAndVisible()
return true
}
I've removed the entry for Main storyboard from the info.plist file, as well as the 'Main Interface' entry in the General settings.
I'm using Swift 3 and targeting an iOS 8 device. I'm using XCode 8.3.1.
There is no output in the console, and there are no exceptions. The viewDidLoad function is even triggering on breakpoint, so the codepath seems to be running correctly.
Any ideas?
Here's the bare bones code for DummyViewController upon request.
class DummyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
General settings showing no reference to Main Interface
Here is the image for the .xib linked to DummyViewController
** The solution to get around this case is to manually specify the .xib to load for the DummyViewController **
It looks like the ViewController is not set to display anything. Unless you are using a xib (in which case you would need to load the view controller in a different way, see below), there is nothing describing how the ViewController's view should render.
To test this out, you can add the line self.view.backgroundColor = UIColor.red to the ViewController's viewDidLoad() method, then run it again on the device- if the background color turns red, then hooray! The next step will be programmatically adding a UILabel.
Loading UIViewController From a XIB
let vc = MyViewController(nibName: "xibname", bundle: nil)
Alternatively, you can mask the loading by adding a custom init inside MyViewController:
class MyViewController: UIViewController {
required init() {
super.init(nibName: "xibname", bundle: nil)
}
}
(Thank you zonily-jame for the addition of hiding it in the class)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginView : BaseClassVC = mainStoryboardIpad.instantiateViewControllerWithIdentifier("BaseClassVC") as BaseClassVC
let navigationController = UINavigationController(rootViewController: loginView)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
change Your window root as and set color
let viewController:DummyViewController = DummyViewController()
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = viewController
And change your controller
override func viewDidLoad() {
self.view.backgroundColor = UIColor.white
}
You just need to initialize viewcontroller object and implement white background color to its instance.
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
//Add below 2 lines
let vc = DummyViewController()
vc.view.backgroundColor = .white
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
let tabBarController = self.window!.rootViewController as! UITabBarController
let tabBar = tabBarController.tabBar as UITabBar
let tabBarItem0 = tabBar.items![0] as! UITabBarItem
let tabBarItem1 = tabBar.items![1] as! UITabBarItem
let tabBarItem2 = tabBar.items![2] as! UITabBarItem
let tabBarItem3 = tabBar.items![3] as! UITabBarItem
tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
tabBarItem0.title = "Home"
tabBarItem1.title = "Search"
tabBarItem2.title = "User"
I am new to swift. I have configured the tab bar controller in appdelegate.Now I need to set the rootview controller here and I need to show the tabbar in all my viewcontrollers that I declare.
This will help you. Try to set navigation inside of tab bar controller. Give tab bar item to navigation controller. Like:
Output is:
When you press button:
You can create tabbarcontroller like below, using Xib
//MARK: didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = UITabBarController()
let tabViewController1 = FirstTabViewController(nibName: "FirstTabViewController", bundle: nil)
let tabViewController2 = SecondViewController(nibName:"SecondViewController", bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home_icon"),tag: 1)
tabViewController2.tabBarItem = UITabBarItem(title: "Search",image:UIImage(named: "search_icon") ,tag:2)
tabBarController.viewControllers = [tabViewController1,tabViewController2]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
As per my understanding, you want to achieve something like this :
UITabBarController --> for each tab there will be a navigation controller whose Root will be a VC having tab bar
So I would suggest you to directly use UITabBarControllers for your each root of navigation controller.
That means your root will be UITabBarController, then for each tab there will be UINavigationController whose first view controller will be again a UITabBarController.
For more understanding see below figure. It's only showing flow for one tab of UITabBarController. Repeat the same for all your other tabs.

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.

Resources