swift - 点击远程通知后导航栏丢失

标签 swift remote-notifications

我在应用程序中有一个侧面菜单,因此在单击通知时我导航到 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/

相关文章:

ios - iOS中的远程通知和静默通知有什么区别?

ios - 当远程通知到达时哪个委托(delegate)首先调用,而我的应用程序处于终止模式(不在后台)

ios - 如何将 UIBezierPath 矩形的起点和终点更改为 CAShapeLayer

ios - 基于 customObject 属性订购 Collection View 单元格 - Swift 4 -Xcode 9

ios - 如何清除应用中的远程通知?

ios - 如何根据远程通知类型将用户重定向到各种屏幕

ios - 加载 usdz 文件时出现内存问题

ios - 我如何 swift 绕过我的应用程序窗口的角落?

快速使动画效果永久化

ios - 自定义推送通知设计