自iOS 13

时间:2019-09-27 09:44:34

标签: swift uialertcontroller ios13

由于我使用的是iOS 13,所以我的每个UIAlertController都会显示大约半秒钟,并在用户执行任何操作之前会立即消失。有什么想法吗?

当我从应用程序的不同部分使用UIAlertController时,我使用了一个扩展,该扩展允许我同时从经典视图和collectionView(单元格,标头等)弹出

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

这是此扩展名用法的示例:

fileprivate func showMissingAlert() {
        let alert = UIAlertController(title: "blablabla", message: "blablablablabla blabla", preferredStyle: UIAlertController.Style.alert)
        alert.show()
        alert.view.tintColor = Theme.mainAccentColor
        let cancelAction = UIAlertAction(title: "OK, blabla", style: .default, handler: {(alert: UIAlertAction!) in print("ok, leave")})
        alert.addAction(cancelAction)
    }

进一步在我的代码中:

showMissingAlert()

在iOS 13之前,每个UIAlert都可以正常工作...自从我转移到iOS 13甚至iOS 13.1以来,情况就变得一团糟...:(

  • 关于可能导致这种情况的任何想法?

  • 以及如何防止将UIAlert用作潜意识信息:)?

3 个答案:

答案 0 :(得分:8)

我遇到了完全相同的问题,并通过将显示警报的窗口保持在一个强变量中来解决该问题。

例如,您可以在AppDelegate中保留一个用于显示警报的窗口,并在UIAlertController扩展中使用它。

//In app delegate
let alertWindow: UIWindow = {
    let win = UIWindow(frame: UIScreen.main.bounds)
    win.windowLevel = UIWindow.Level.alert + 1
    return win
}()

然后,在您的扩展程序中:

public extension UIAlertController {
    func show() {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        vc.view.tintColor = Theme.mainAccentColor
        appDelegate.alertWindow.rootViewController = vc
        appDelegate.alertWindow.makeKeyAndVisible()
        vc.present(self, animated: true, completion: nil)
    }
}

您还需要确保在关闭警报时从视图中删除警报窗口,否则您的应用将变得无响应,因为所有轻按都将由(不可见的)警报窗口处理,这仍然是一切。 为此,我将以下代码添加到警报中所有操作的处理程序中:

(UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true

答案 1 :(得分:2)

您也可以尝试此解决方案。是我的工作。

在您的班级中写下面的方法。

func presentViewController(alertController: UIAlertController, completion: (() -> Void)? = nil) {
        if var topController = UIApplication.shared.keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }

            DispatchQueue.main.async {
                topController.present(alertController, animated: true, completion: completion)
            }
        }
    }

然后从您的代码中调用它,如下所示

let alertController = UIAlertController(title: "Discard Photo?",
                                                message: "Your photo will not be attached",
                                                preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Keep Photo", style: .default, handler: nil))
        alertController.addAction(UIAlertAction(title: "Discard", style: .default) { (_) -> Void in
            self.PhotoStack.deletePhoto(at: index)
            self.cameraBtn.isEnabled = true
        })

        self.presentViewController(alertController: alertController)

答案 2 :(得分:0)

基于pepsy answer。 如果您不想关心alertWindow.isHidden = true stuff,可以执行以下操作:

class AlertHandler {
    private static let alertWindow: UIWindow = {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.windowLevel = UIWindow.Level.alert + 1
        return window
    }()

    private var alertController: UIAlertController

    init(title: String?,
         message: String?) {
        alertController = UIAlertController(title: title,
                                            message: message,
                                            preferredStyle: .alert)
    }

    func addAction(title: String?,
                   style: UIAlertAction.Style,
                   handler: ((UIAlertAction) -> Void)? = nil) {
        let action = UIAlertAction(title: title,
                                   style: style) { action in
                                    handler?(action)
                                    AlertHandler.alertWindow.isHidden = true
        }
        alertController.addAction(action)
    }

    func present() {
        AlertHandler.alertWindow.rootViewController = UIViewController()
        AlertHandler.alertWindow.makeKeyAndVisible()
        AlertHandler.alertWindow.rootViewController?.present(alertController,
                                                             animated: true,
                                                             completion: nil)
    }
}