gpt4 book ai didi

ios - 在所有内容之上显示一个 UIAlertController 并保持在顶部,即使推送另一个 View (iOS 13)

转载 作者:行者123 更新时间:2023-12-01 22:00:15 24 4
gpt4 key购买 nike

我正在尝试实现 UIAlertController即使应用程序尝试推送另一个 View Controller ,它也会显示在窗口顶部并保持这种状态直到用户关闭它。

我已经阅读了这些问题和答案:

  • ios - present UIAlertController on top of everything regardless of the view hierarchy
  • Present ViewController on top of everything regardless of the view hierarchy in IOS 13

  • 虽然在 iOS 13 之前的版本中,所提供的解决方案就像一个魅力,但我一直试图在 iOS 13 中解决这个问题:

    当我显示 UIAlertController ,它保持在顶部,直到另一个 View Controller 被插入导航堆栈。如果发生这种情况, UIAlertController消失。

    func showHighPriorityNotification() {
    let alertWindow = UIWindow(frame: UIScreen.main.bounds)
    alertWindow.rootViewController = UIViewController()
    alertWindow.windowLevel = UIWindowLevelAlert + 1
    alertWindow.makeKeyAndVisible()

    let alertController = UIAlertController(title: "Title"), message: "Message"), preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Button"), style: .default))

    if #available(iOS 13, *) {
    // Trying to get this functionality to work in iOS 13.
    if var topController = alertWindow.rootViewController {
    while let presentedViewController = topController.presentedViewController {
    topController = presentedViewController
    }
    topController.modalPresentationStyle = .fullScreen
    topController.present(alertController, animated: true)
    }
    } else {
    // This code works in iOS 12 and below, but NOT in iOS 13.
    alertWindow.rootViewController?.present(alertController, animated: true)
    }
    }

    在iOS13中,有没有办法保留 UIAlertController在顶部并允许 View 被推到它下面?正如我所说,在以前的版本中,这很好用。

    最佳答案

    您可以使用与在 iOS 12 及更低版本中使用的相同的实现,但您必须对显示警报的窗口进行强引用。

    在您当前的代码中 - 在 iOS 13 上运行时 - alertWindow将尽快销毁showHighPriorityNotification完成,解除警报。可以通过强烈引用 alertWindow 来修复它。别的地方。

    查看this answer一种实现方式。

    关于ios - 在所有内容之上显示一个 UIAlertController 并保持在顶部,即使推送另一个 View (iOS 13),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60227342/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com