Skip to content

Instantly share code, notes, and snippets.

@iranjith4
Created October 30, 2024 09:44
Show Gist options
  • Save iranjith4/34aa676675a4fa94ee5ecbe403152564 to your computer and use it in GitHub Desktop.
Save iranjith4/34aa676675a4fa94ee5ecbe403152564 to your computer and use it in GitHub Desktop.
import UIKit
public enum NavigationHelper {
public static func popToRoot(
animated: Bool = false
) {
getNavigationController(viewController: UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }?.rootViewController)?.popToRootViewController(animated: animated)
}
public static func pop(
animated: Bool = false
) {
getNavigationController(viewController: UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }?.rootViewController)?.popViewController(animated: true)
}
public static func getNavigationController(viewController: UIViewController?) -> UINavigationController? {
guard let viewController = viewController else {
return nil
}
if let navigationController = viewController as? UITabBarController {
return getNavigationController(viewController: navigationController.selectedViewController)
}
if let navigationController = viewController as? UINavigationController {
return navigationController
}
for childViewController in viewController.children {
return getNavigationController(viewController: childViewController)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment