'open viewcontroller embedded in navigation view in swift UIKit
I'm trying to open my view controller (with storyboard) which is embedded in a navigation view from the scende delegate
i am doing something like this
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.window?.rootViewController = homePage
however i can't see the navigation bar, i mean, i just see the viewcontroller content i need to open the navigation view as well
thanks!
Solution 1:[1]
Is HomeViewController the name of your UINavigationViewController ?
If not, then this is expected.
Give your UINavigationViewController an identifier in the storyboard like you did for HomeViewController and then replace:
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "YourNavControllerIdenfier") as! UINavigationController
self.window?.rootViewController = homePage
Try this and see if it gives you the result you want
Solution 2:[2]
Firstly, you need to make your NavigationController as the root vc, not the HomeViewController;
Secondly, the default Navigation bar style is transparent, if you want to see it, you can customise it, for example:
self.navigationController?.navigationBar.barStyle = .black
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shawn Frank |
| Solution 2 | user9129939 |
