我已将启动屏幕设置为 Root View ,同时我的应用程序向 firebase 发出请求,然后在请求完成后重新分配 Root View 。不幸的是,当我这样做时,我的导航栏消失或被新的 Root View 覆盖。当我运行模拟器时,我可以短暂地看到导航栏,然后它被我的 TableViewController 覆盖了。我该如何防止这种情况发生?
这是我在 AppDelegate 中实现这一切的代码:
var window: UIWindow?
let searchManager = SearchManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
makeRootViewLaunchScreen()
FirebaseApp.configure()
searchManager.getMosaicTitles { results in
self.searchManager.listOfMosaics = results
self.stopDisplayingLaunchScreen()
}
// Adds border to bottom of the nav bar
UINavigationBar.appearance().shadowImage = UIImage.imageWithColor(color: UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0))
// Override point for customization after application launch.
return true
}
func makeRootViewLaunchScreen() {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "launchScreen")
UIApplication.shared.keyWindow?.rootViewController = viewController
}
func stopDisplayingLaunchScreen() {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "centralViewController") as? SearchResultsTableViewController
viewController?.searchManager = searchManager
UIApplication.shared.keyWindow?.rootViewController = viewController
UIApplication.shared.keyWindow?.rootViewController?.navigationController?.isNavigationBarHidden = false
}
如您所见,我尝试使用 UIApplication.shared.keyWindow?.rootViewController?.navigationController?.isNavigationBarHidden = false
强制显示导航栏,但它没有出现。我的应用程序仍然如下所示:
最佳答案
您正在将 UIViewController
设置为您的 Root Controller 。您要做的是将 UINavigationController
设置为您的 Root Controller 。
要么在你的 Storyboard中创建一个新的导航 Controller 并加载它而不是加载“centralViewController”,要么像这样修改你的函数:
func stopDisplayingLaunchScreen() {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "centralViewController") as? SearchResultsTableViewController {
viewController.searchManager = searchManager
// create a new UINavigationController
let newNavVC = UINavigationController()
// set the "root" VC of the NavVC to your SearchResultsTableViewController
newNavVC.setViewControllers([viewController], animated: false)
// use the new NavVC as the new rootViewController
UIApplication.shared.keyWindow?.rootViewController = newNavVC
UIApplication.shared.keyWindow?.rootViewController?.navigationController?.isNavigationBarHidden = false
}
}
注意:未经测试,但这应该可以帮助您上路。
编辑:另一种方法......
不是交换键窗口的根 Controller ,而是创建一个 ViewController 来指示您正在初始化/检索数据。该 VC 可以是导航 Controller 的“根”。
将您的searchManager.getMosaicTitles
函数放在那个 VC 中。完成后,将 Nav Controller 中的当前“启动” Controller 替换为 TableVC。它看起来像这样:
// note: searchManager will have to be a reference back to AppDelegate.searchManager
searchManager.getMosaicTitles { results in
self.searchManager.listOfMosaics = results
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "centralViewController") as? SearchResultsTableViewController {
viewController.searchManager = searchManager
// replace the current Nav VC stack with the new SearchResultsTableViewController
self.navigationController.setViewControllers([viewController], animated: false)
}
}
关于ios - Navigation Bar 消失 Change Root ViewController 然后重新赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46814825/