我在应用程序中有一个侧面菜单,因此在单击通知时我导航到 View Controller 但无法导航到其他 VC,因为它没有显示导航栏。
我已经在我的 App Delegate 中完成了此操作,并且在我的 Storyboard 中也有一个导航 Controller 。
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
window?.rootViewController = otherVC;
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler:@escaping (UIBackgroundFetchResult) -> Void) {
let state = application.applicationState
if state == .inactive || state == .background {
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
window?.rootViewController = otherVC
print("background")
} else {
print("foreground")
}
}
最佳答案
当然去掉了导航栏; View Controller 需要嵌入到导航 Controller 中。
试试这个:
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
// (actually, you don't need to cast to MessageViewController here;
// the returned reference already is guaranteed to at least be a
// UIViewController instance; you don't need more specificity in this
// case --i.e, embed in navigation)
let navigation = UINavigationController(rootViewController: otherVC)
window?.rootViewController = navigation
注意:这将呈现一个以您的 View Controller 为根的全新导航。从应用程序的初始屏幕手动导航时,您将无法“导航回”到通常位于它之前的任何其他屏幕。
如果您需要在您的导航层次结构中重新创建一个特定位置,您将需要实例化所有以前的 View Controller 直到您想要显示的那个,并立即将它们设置为导航堆栈。
虚拟示例:
let first = UIStoryboard(name: "First", bundle: nil).instantiateInitialViewController()
let second = UIStoryboard(name: "Second", bundle: nil).instantiateInitialViewController()
let top = UIStoryboard(name: "Mail", bundle: nil).instantiateInitialViewController()
let vcs = [first, second, top]
let navigation = UINavigationController()
navigation.setViewController(vcs, animated: false)
window?.rootViewController = navigation
(此示例假设每个 VC 都位于单独的 Storyboard中,但如果适合您的设置,您也可以使用 instantiateViewController(withIdentifier:_)
)
现在,您可以使用导航 Controller 的后退按钮返回“第二”和“第一”。
关于swift - 点击远程通知后导航栏丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750467/