点击通知跳转指定ViewController后导航栏消失问题求助
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 letto 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
navigationItemproperties 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
相关产品推荐
相关产品推荐

