1

I have a view which is subview of a keyWindow (as you can see in the below code). I would like to show an alertController on top of this view (myView).

I tried to present alertController on the topViewController (as you can see below), this did not work since the view I have is not part of the topViewController.

How can I present alertController on top of a view ?

This is how I added a view to the keyWindow

guard let window = UIApplication.shared.keyWindow else{
    return
}

window.addSubview(myView)

The below did not work since the view is not part of the topViewController.

guard let vc = UIApplication.topViewController() else { return}
vc.present(alertController, animated: true, completion: nil)
SpaceX
  • 2,814
  • 2
  • 42
  • 68

1 Answers1

0

try this

func showAlertMessage(title:String? = nil, _ message:String) {
    let rootVC = APP_DELEGATE.window?.topMostController()
    var alert = UIAlertController(title: message, message: nil, preferredStyle: .alert)
    if let title = title {
        alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    }
    alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
    rootVC?.present(alert, animated: true, completion: nil)

}

rv7284
  • 1,092
  • 9
  • 25