Last active
February 14, 2024 09:56
-
-
Save vladdeSV/42e418c4a01696d3622da4a640b4ab61 to your computer and use it in GitHub Desktop.
SwiftUI, get top view controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIApplication+TopViewController.swift | |
// Ticketing & Sales | |
// | |
// Created by Vladimirs Nordholm on 2024-02-14. | |
// | |
import SwiftUI | |
extension UIApplication { | |
public var topViewController: UIViewController? { | |
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | |
let rootViewController = windowScene.windows.first?.rootViewController else { | |
return nil | |
} | |
var topViewController = rootViewController | |
while let presentedViewController = topViewController.presentedViewController { | |
topViewController = presentedViewController | |
} | |
return topViewController | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
UIApplication.shared.topViewController