在swift中,如何让UIViewController().view保持在顶部?比如UIAlertController

In swift, How to keep a UIViewController().view at the top?like UIAlertController

我正在使用 CustomAlertViewController 作为 UIViewController 上的警报。当前要呈现的代码片段 CustomAlertViewController -

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

但我想在视图层次结构的顶部添加CustomAlertViewController

有什么实现方法的建议吗?

可能您想将 UIViewCotroller 的视图添加为键 window 的子视图。检查这个答案:

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)
        } 
 }

如果你调用这个函数

 showAlertConfirm(errorMessage: "Test", operation:
    requestData()
)