Created
October 7, 2021 09:35
-
-
Save odnaks/3f3fd0d20f318c6276e76d0f9d7de5a7 to your computer and use it in GitHub Desktop.
Alert controller which always top
This file contains 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
/// Alert controller which always top. You dont need to bind it to controller. | |
/// Just call show method. And TopAlertController will above any controller in hierarchy. | |
import UIKit | |
// MARK: - TopAlertController | |
final class TopAlertController: UIAlertController { | |
// MARK: - Private properties | |
private lazy var alertWindow: UIWindow = { | |
let window = UIWindow(frame: UIScreen.main.bounds) | |
window.rootViewController = ClearViewController() | |
window.backgroundColor = UIColor.clear | |
window.windowLevel = UIWindow.Level.alert | |
return window | |
}() | |
// MARK: - Internal methods | |
func show(animated flag: Bool = true, completion: (() -> Void)? = nil) { | |
guard let rootVC = alertWindow.rootViewController else { return } | |
alertWindow.makeKeyAndVisible() | |
rootVC.present(self, animated: flag, completion: completion) | |
} | |
} | |
// MARK: - ClearViewController | |
fileprivate class ClearViewController: UIViewController { | |
override var preferredStatusBarStyle: UIStatusBarStyle { | |
UIApplication.shared.statusBarStyle | |
} | |
override var prefersStatusBarHidden: Bool { | |
UIApplication.shared.isStatusBarHidden | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment