Skip to content

Instantly share code, notes, and snippets.

@odnaks
Created October 7, 2021 09:35
Show Gist options
  • Save odnaks/3f3fd0d20f318c6276e76d0f9d7de5a7 to your computer and use it in GitHub Desktop.
Save odnaks/3f3fd0d20f318c6276e76d0f9d7de5a7 to your computer and use it in GitHub Desktop.
Alert controller which always top
/// 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