This worked for me but I can't find the url so I will just share the code. You can also try it out...
Create a file and add the following;
import Foundation
struct NavigationUtil {
    static func popToRootView() {
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            findNavigationController(viewController: 
UIApplication.shared.windows.filter { $0.isKeyWindow 
}.first?.rootViewController)?
                .popToRootViewController(animated: true)
        }
    }
static func findNavigationController(viewController: UIViewController?) 
-> UINavigationController? {
        guard let viewController = viewController else {
            return nil
        }
if let navigationController = viewController as? UINavigationController 
{
        return navigationController
    }
for childViewController in viewController.children {
        return findNavigationController(viewController: 
childViewController)
    }
return nil
    }
}
Then call this from anywhere in your code;
NavigationUtil.popToRootView()