【问题标题】:Present UIAlertView on window在窗口上显示 UIAlertView
【发布时间】:2020-01-31 14:05:05
【问题描述】:

有可能,我的控制器上有自定义视图,我必须在其上显示警报。所以我使用下面的扩展在窗口而不是任何 UIViewController 上呈现控制器。

扩展

extension UIViewController {
    func presentControllerToWindow(){
        let win = UIWindow(frame: UIScreen.main.bounds)
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        win.rootViewController = vc
        win.windowLevel = UIWindow.Level.alert + 1
        win.makeKeyAndVisible()
        vc.present(self, animated: true, completion: nil)
    }
}

当前 AlertController

let alertController = UIAlertController(title: nil, message: "Select Option", preferredStyle: .alert)

alertController.presentControllerToWindow()

问题:

代码在 swift 4.X 之前运行良好,但在 swift 5.X 中,警报控制器会在另一秒出现并自动关闭。

GIF:

  1. OpenPicker 将自定义视图添加为子视图。
  2. 点击添加文件后,我将展示 alertcontroller。
  3. 它会自动关闭。

编辑:

我正在添加我的自定义视图,如下所示。

extension UIView {

    func addToWindow()  {
        let window = UIApplication.shared.keyWindow!
        self.frame = window.bounds
        window.makeKeyAndVisible()
        window.windowLevel = window.windowLevel + 1
        window.addSubview(self)
    }
}

let customView = MyCustomView()
customView.addToWindow()

现在结束这个MyCustomView,我需要显示UIAlertController

【问题讨论】:

  • 你不应该触摸窗口,你可以找到顶部视图控制器并直接在那里显示警报。
  • @PrashantTukadiya 窗口有什么问题?
  • @jalone 没问题,但这不是一个好习惯 - 可能存在多窗口应用程序之类的场景 - 并且有可能与应用程序的某些本机控件发生冲突 - 警报视图应该从应用中可见的最顶部视图

标签: ios windows uialertcontroller swift5


【解决方案1】:

您必须在 UIApplication keyWindow

中添加您的 UIViewController 视图
extension UIViewController {
    func presentControllerToWindow(){
        let win = UIWindow(frame: UIScreen.main.bounds)
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        win.rootViewController = vc
        win.windowLevel = UIWindow.Level.alert + 1
        win.makeKeyAndVisible()
        UIApplication.shared.keyWindow?.addSubview(vc.view) //added
        vc.present(self, animated: true, completion: nil)
    }
}

【讨论】:

  • iOS 13 引入了新样式来呈现视图控制器。要关闭控制器,用户可以向下滑动控制器,这会导致与我正在使用的图像缩放功能发生冲突。你知道怎么处理吗?
  • 你可以使用vc.modalPresentationStyle = .fullScreen来避免这种情况。
  • @palme,我试过.fullScreen,但没有任何改变。你能看看 -> i.stack.imgur.com/IudLC.gif
  • @dahiya_boy 让我搜索该问题。如果我发现了什么,请告诉你。
  • @dahiya_boy 看看 vc.isModalInPresentation = true 属性 (developer.apple.com/documentation/uikit/uiviewcontroller/…) 还添加 #available(iOS 13.0, *),因为这是 iOS 13 的功能。但是,您似乎为错误的控制器设置了 modalPresentationStyle,因为它在 gif 中的显示方式是错误的。
【解决方案2】:

我个人使用以下扩展程序来创建警报。

extension UIViewController {
  func showAlert(withTitle title: String?, message: String?) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
  }
}

它适用于 iOS 13

【讨论】:

  • 是的,bcz 您在 UIViewController 上添加警报,而不是在窗口上。我在控制器和我的控制器上有一个自定义视图,还有 4 个按钮。
  • 我现在明白了。对困惑感到抱歉。这个线程有帮助吗? Apple
  • 兄弟你的代码不符合我的要求。我已经编辑了我的帖子,你会更清楚这个场景。
  • 查看答案here
  • 嘿,它正在工作,但有任何问题。现在我必须冗余地写(UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true。我的意思是在我的项目中从 100 年代开始调用相同的扩展名,现在很难从我必须隐藏 alertWindow 的地方追踪它。你知道有没有任何关于 alertController 解除的回调,以便可以集中管理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多