当我点击推送通知并在swift中移动ViewController时,为什么状态栏会消失

时间:2017-03-22 02:56:27

标签: ios swift swift3

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    /* */
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = UIViewController()
    self.window?.windowLevel = UIWindowLevelAlert + 1
    self.window?.makeKeyAndVisible()

    /*

    fetch and add push notification data

     */
    goAnotherVC()
}

func goAnotherVC() {
    if (application.applicationState == UIApplicationState.active) {
        /* active stage is working */ 
    } else if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) {
        if (type == "1" || type == "2") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "3") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "4") {
            let storyboard: UIStoryboard = UIStoryboard(name: "Enquiry", bundle: nil)
            let enqVC = storyboard.instantiateViewController(withIdentifier: "EnquiryDetailViewController") as! EnquiryDetailViewController
            let navigationController = UINavigationController.init(rootViewController: enqVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        }
    }
}

上面的代码是获取推送通知数据,并在用户点击它时发送到相关的View Controller。但是我发现View Controller打开时状态栏丢失了。请让我知道如何解决它,谢谢。

enter image description here

1 个答案:

答案 0 :(得分:1)

首先看一眼(并且没有实际验证代码),我怀疑罪魁祸首就是:

self.window = UIWindow(frame: UIScreen.main.bounds)

您正在设置窗口以覆盖视图的完整边界。要查看这是否是问题,只需将该行更改为以下内容:

var rect = UIScreen.main.bounds
rect.origin.y += 20
self.window = UIWindow(frame: rect)

看看问题是否消失了。在我的代码中,我只是更改了窗口的顶部位置以允许状态栏。