如何检测模态视图在 SwiftUI 中全局可见
How to detect a modal view is visible globally in SwiftUI
在 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报了
的错误
`` [Presentation] 尝试在 <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentGS1_GS1_GS1_GS1_V16BuyersCircle_iOS11ContentViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_11UserService___GS4_GSqCS2_11GlobalModal___GS4_GSqCS2_9CartModel___GS4_GSqCS2_19ReachabilityMonitor___GS4_GSqCS2_19PartialSheetManager___: 0x158715600> 上呈现 (来自
所以我在想
- 如果我能得到当前的SwiftUI modal view controller,那么我就可以呈现基于它的UIKit modal。我不知道如何让它工作。
在您的算法中,您正在计算 rootViewController
,但随后将 self
呈现给 UIApplication.shared.rootViewController
,而不是找到的 rootViewController
替换
UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
有
rootViewController?.present(self, animated: animated, completion: completion)
在 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报了
的错误`` [Presentation] 尝试在 <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentGS1_GS1_GS1_GS1_V16BuyersCircle_iOS11ContentViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_11UserService___GS4_GSqCS2_11GlobalModal___GS4_GSqCS2_9CartModel___GS4_GSqCS2_19ReachabilityMonitor___GS4_GSqCS2_19PartialSheetManager___: 0x158715600> 上呈现
所以我在想
- 如果我能得到当前的SwiftUI modal view controller,那么我就可以呈现基于它的UIKit modal。我不知道如何让它工作。
在您的算法中,您正在计算 rootViewController
,但随后将 self
呈现给 UIApplication.shared.rootViewController
,而不是找到的 rootViewController
替换
UIApplication.shared.rootViewController?.present(self, animated: animated, completion: completion)
有
rootViewController?.present(self, animated: animated, completion: completion)