当我的应用程序向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
强制导航栏出现,但它并没有出现。我的应用仍然如下所示:
答案 0 :(得分:1)
您正在设置UIViewController
作为根控制器。您要做的是将UINavigationController
设置为根控制器。
在故事板中创建一个新的导航控制器并加载该导航控制器而不是加载“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
}
}
注意:未经测试,但这应该可以帮助您。
编辑:另一种方法......
创建一个指示您正在初始化/检索数据的ViewController,而不是交换关键窗口的根控制器。 VC可以是导航控制器的“根”。
将您的searchManager.getMosaicTitles
功能放在 VC中。完成后,使用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)
}
}
答案 1 :(得分:0)
要防止出现导航栏控制器和tableView出现之间的延迟,请查看以下内容。 Prevent navigation bar from disappearing without lag