-2

I'm using CustomAlertViewController as an Alert on UIViewController. Current code snippet to present CustomAlertViewController -

CustomAlertViewController: UIViewController {}
self.present(CustomAlertViewController(), animated: true, completion: {})

But I want to addCustomAlertViewControlleron top of the view hierarchy.

Any suggestion how to achieve this?

Amir Khan
  • 1,318
  • 1
  • 14
  • 39
Erwin
  • 11
  • 3

2 Answers2

0

Probably you want to add your UIViewCotroller’s view as a subview of key window. Check this answer: https://stackoverflow.com/a/38540271/5779168

tahavath
  • 234
  • 2
  • 9
0
extension UIViewController {
  func showAlertConfirm(errorMessage: String, operation: @escaping ()->()) {
  let alert = UIAlertController(title: "Confirmation", message: errorMessage, 
  preferredStyle: .alert)

  let confirmationAction = UIAlertAction(title: "OK", style: .default) { (action) 
         in
           operation()
   }
 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:nil)
    alert.addAction(confirmationAction)
    alert.addAction(cancelAction)
    present(alert, animated: true, completion: nil)
        } 
 }

if you call this function

 showAlertConfirm(errorMessage: "Test", operation:
    requestData()
)
Puji Wahono
  • 338
  • 5
  • 10