【问题标题】:How to detect a modal view is visible globally in SwiftUI如何检测模态视图在 SwiftUI 中全局可见
【发布时间】:2021-07-30 21:27:36
【问题描述】:

在 SwiftUI 中,有几种方法可以呈现模态视图,例如 .popover

我的背景是我想在其他地方而不是在当前视图页面下展示 UIKit 模式视图

private func presentGlobally(animated: Bool, completion: (() -> Void)?) {
        var rootViewController = UIApplication.shared.rootViewController
        while true {
          if let presented = rootViewController?.presentedViewController {
            rootViewController = presented
          } else if let navigationController = rootViewController as? UINavigationController {
            rootViewController = navigationController.visibleViewController
          } else if let tabBarController = rootViewController as? UITabBarController {
            rootViewController = tabBarController.selectedViewController
          } else {
            break
          }
        }
        UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
    }

上述方法不起作用,因为 SwiftUI 给出了错误

``[演示]尝试本上 TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentGS1_GS1_GS1_GS1_V16BuyersCircle_iOS11ContentViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_11UserService ___ GS4_GSqCS2_11GlobalModal ___ GS4_GSqCS2_9CartModel ___ GS4_GSqCS2_19ReachabilityMonitor ___ GS4_GSqCS2_19PartialSheetManager EM> ___:0x158715600>(距离 P>

所以我在想

  • 如果我能拿到当前的SwiftUI modal view controller,那么我就可以基于它呈现UIKit modal。我不知道如何让它工作。

【问题讨论】:

    标签: ios swiftui uikit


    【解决方案1】:

    在您的算法中,您正在计算 rootViewController,但随后将 self 呈现给 UIApplication.shared.rootViewController,而不是找到的 rootViewController

    替换

    UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
    

    rootViewController?.present(self, animated: animated, completion: completion)
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 2013-03-21
      • 1970-01-01
      • 2020-07-06
      • 2017-07-25
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多