导航栏消失 Change Root ViewController 然后重新赋值
Navigation Bar Disappears Change Root ViewController and then reassign
当我的应用程序向 firebase 发出请求时,我已将启动屏幕设置为根视图,然后在请求完成后重新分配根视图。不幸的是,当我这样做时,我的导航栏消失或被新的根视图覆盖。当我 运行 模拟器时,我可以短暂地看到导航栏,然后它被我的 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
设置为根控制器。你想要做的是设置一个 UINavigationController
作为你的 Root 控制器。
要么在你的故事板中创建一个新的导航控制器并加载它而不是加载 "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
}
}
注意:未经测试,但这应该可以帮助您。
编辑:另一种方法...
不要交换密钥 window 的根控制器,而是创建一个 ViewController 来指示您正在初始化/检索数据。 VC 可以是导航控制器的 "root"。
将你的 searchManager.getMosaicTitles
函数放在 that VC 中。完成后,将导航控制器中的当前 "launch" 控制器替换为 表VC。它看起来像这样:
// 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)
}
}
为了防止出现导航栏控制器和出现 tableView 之间的延迟,请查看以下内容。
当我的应用程序向 firebase 发出请求时,我已将启动屏幕设置为根视图,然后在请求完成后重新分配根视图。不幸的是,当我这样做时,我的导航栏消失或被新的根视图覆盖。当我 运行 模拟器时,我可以短暂地看到导航栏,然后它被我的 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
设置为根控制器。你想要做的是设置一个 UINavigationController
作为你的 Root 控制器。
要么在你的故事板中创建一个新的导航控制器并加载它而不是加载 "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
}
}
注意:未经测试,但这应该可以帮助您。
编辑:另一种方法...
不要交换密钥 window 的根控制器,而是创建一个 ViewController 来指示您正在初始化/检索数据。 VC 可以是导航控制器的 "root"。
将你的 searchManager.getMosaicTitles
函数放在 that VC 中。完成后,将导航控制器中的当前 "launch" 控制器替换为 表VC。它看起来像这样:
// 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)
}
}
为了防止出现导航栏控制器和出现 tableView 之间的延迟,请查看以下内容。