You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击通知跳转指定ViewController后导航栏消失问题求助

Fix: Navigate to Specific ViewController from AppDelegate with Navigation Bar Intact

Hey there, let's get this sorted out—your issue boils down to not properly embedding your target view controller within a navigation controller when handling the notification tap. Here's a step-by-step solution that works for all app states (foreground, background, not running):

1. Handle Notification Tap in AppDelegate

First, make sure you're implementing the correct UserNotifications delegate method in your AppDelegate.swift:

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Request notification permissions and set delegate
        UNUserNotificationCenter.current().delegate = self
        return true
    }

    // This method triggers when user taps the notification
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Get the key window (compatible with iOS 12 and earlier)
        guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else {
            completionHandler()
            return
        }

        // Initialize your target view controller
        // Option A: Programmatic VC
        let targetVC = MyViewController() 
        // Option B: Storyboard VC (replace with your storyboard name and VC identifier)
        // let storyboard = UIStoryboard(name: "Main", bundle: nil)
        // guard let targetVC = storyboard.instantiateViewController(withIdentifier: "MyPostVC") as? MyPostViewController else {
        //     completionHandler()
        //     return
        // }

        // Preserve navigation bar by integrating with navigation controller
        if let existingNav = window.rootViewController as? UINavigationController {
            // Push to existing navigation stack (best for consistent navigation flow)
            existingNav.pushViewController(targetVC, animated: true)
        } else {
            // Wrap target VC in a new navigation controller and set as root
            let newNav = UINavigationController(rootViewController: targetVC)
            window.rootViewController = newNav
            window.makeKeyAndVisible()
        }

        completionHandler()
    }
}

2. Why Your Previous Attempts Failed

  • Navigation bar disappearing: You were likely setting the target VC directly as the window's root view controller (or presenting it without a navigation wrapper). UINavigationBar only appears when the VC is part of a UINavigationController's stack.
  • Couldn't jump to myPostVc: This was probably due to incorrect VC initialization (e.g., wrong storyboard identifier, typos in class names, or failing to cast to the correct VC type). Always use optional binding with guard let to catch these issues.

3. iOS 13+ Note (SceneDelegate)

If your app uses SceneDelegate (standard for iOS 13+), adjust how you access the window by pulling it from the active scene:

// In SceneDelegate.swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    guard let window = self.window else {
        completionHandler()
        return
    }
    // Rest of the VC initialization and navigation setup code goes here
}

Final Checks

  • Verify your target VC has navigationItem properties set (e.g., title, rightBarButtonItem) so the navigation bar shows interactive elements.
  • Test all app states: tap the notification while the app is in foreground, background, and completely closed to ensure consistent behavior.

内容的提问来源于stack exchange,提问作者ken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:03:30