【问题标题】:In swift, How to keep a UIViewController().view at the top?like UIAlertController在swift中,如何在顶部保持一个UIViewController().view?比如UIAlertController
【发布时间】:2019-03-21 05:54:06
【问题描述】:

我在UIViewController 上使用CustomAlertViewController 作为警报。当前代码 sn -p 呈现CustomAlertViewController -

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

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

有什么建议可以实现吗?

【问题讨论】:

  • 什么?解释为什么你需要这样做
  • 添加 Window 而不是在 viewController 上显示。
  • 喜欢 UIAlertController

标签: ios swift uialertcontroller


【解决方案1】:
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()
)

【讨论】:

    【解决方案2】:

    可能您想将 UIViewCotroller 的视图添加为关键窗口的子视图。检查这个答案:https://stackoverflow.com/a/38540271/5779168

    【讨论】:

    • 但是这个视图不能控制器
    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多