如何忽略 SwiftUI 模态背景,或使模态背景 clear/transparent?
How to ignore SwiftUI modal background, or make modal background clear/transparent?
目前我们为 swiftUI 模态提供免费的不透明 white/black 背景。无论如何要删除自由的不透明颜色并使模态视图透明?
在下图中,即使呈现模态,最终结果也应该能够看到图像。
基于此 code snippet 您可以创建 viewcontroller
扩展并修改您的演示文稿。
这是修改后的代码:
struct ViewControllerHolder {
weak var value: UIViewController?
init(_ value: UIViewController?) {
self.value = value
}
}
struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }
}
extension EnvironmentValues {
var viewController: ViewControllerHolder {
get { return self[ViewControllerKey.self] }
set { self[ViewControllerKey.self] = newValue }
}
}
extension UIViewController {
func present<Content: View>(presentationStyle: UIModalPresentationStyle = .automatic, transitionStyle: UIModalTransitionStyle = .coverVertical, animated: Bool = true, completion: @escaping () -> Void = {}, @ViewBuilder builder: () -> Content) {
let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
toPresent.modalPresentationStyle = presentationStyle
toPresent.rootView = AnyView(
builder()
.environment(\.viewController, ViewControllerHolder(toPresent))
)
toPresent.view.backgroundColor = .clear // This line is modified
self.present(toPresent, animated: animated, completion: completion)
}
}
您的 SwiftUI ContentView:
struct ContentView: View {
@Environment(\.viewController) private var viewControllerHolder: ViewControllerHolder
private var viewController: UIViewController? {
self.viewControllerHolder.value
}
var body: some View {
ZStack {
Color.red
Button(action: {
self.viewController?.present(builder: {
Text("OK")
})
}) {
Text("Present me!")
}
}
}
}
目前我们为 swiftUI 模态提供免费的不透明 white/black 背景。无论如何要删除自由的不透明颜色并使模态视图透明?
在下图中,即使呈现模态,最终结果也应该能够看到图像。
基于此 code snippet 您可以创建 viewcontroller
扩展并修改您的演示文稿。
这是修改后的代码:
struct ViewControllerHolder {
weak var value: UIViewController?
init(_ value: UIViewController?) {
self.value = value
}
}
struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }
}
extension EnvironmentValues {
var viewController: ViewControllerHolder {
get { return self[ViewControllerKey.self] }
set { self[ViewControllerKey.self] = newValue }
}
}
extension UIViewController {
func present<Content: View>(presentationStyle: UIModalPresentationStyle = .automatic, transitionStyle: UIModalTransitionStyle = .coverVertical, animated: Bool = true, completion: @escaping () -> Void = {}, @ViewBuilder builder: () -> Content) {
let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
toPresent.modalPresentationStyle = presentationStyle
toPresent.rootView = AnyView(
builder()
.environment(\.viewController, ViewControllerHolder(toPresent))
)
toPresent.view.backgroundColor = .clear // This line is modified
self.present(toPresent, animated: animated, completion: completion)
}
}
您的 SwiftUI ContentView:
struct ContentView: View {
@Environment(\.viewController) private var viewControllerHolder: ViewControllerHolder
private var viewController: UIViewController? {
self.viewControllerHolder.value
}
var body: some View {
ZStack {
Color.red
Button(action: {
self.viewController?.present(builder: {
Text("OK")
})
}) {
Text("Present me!")
}
}
}
}