gpt4 book ai didi

ios - 如何在 swiftUI 中呈现 crossDissolve View ?

转载 作者:行者123 更新时间:2023-12-01 16:12:48 25 4
gpt4 key购买 nike

在 UIKit 中,我使用下面的代码来呈现具有演示风格的模态视图 Controller 交叉溶解

controller.modalTransitionStyle = .crossDissolve
controller.modalPresentationStyle = .overFullScreen
UIApplication.topViewController()?.present(controller, animated: true, completion: nil)

但是我如何在 swiftUI 中实现这一点?

最佳答案

您可以像这样在 UIViewController 上进行扩展:


struct ViewControllerHolder {
weak var value: UIViewController?
}

struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder {
return ViewControllerHolder(value: UIApplication.shared.windows.first?.rootViewController)

}
}

extension EnvironmentValues {
var viewController: UIViewController? {
get { return self[ViewControllerKey.self].value }
set { self[ViewControllerKey.self].value = newValue }
}
}

extension UIViewController {
func present<Content: View>(style: UIModalPresentationStyle = .automatic, transitionStyle: UIModalTransitionStyle = .coverVertical, @ViewBuilder builder: () -> Content) {
let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
toPresent.modalPresentationStyle = style
toPresent.modalTransitionStyle = transitionStyle
toPresent.rootView = AnyView(
builder()
.environment(\.viewController, toPresent)
)
self.present(toPresent, animated: true, completion: nil)
}
}


并在 SwiftUI 代码中随意使用它:

struct ContentView: View {

@Environment(\.viewController) private var viewControllerHolder: UIViewController?
private var viewController: UIViewController? {
self.viewControllerHolder
}

var body: some View {
Button(action: {
self.viewController?.present(style: .fullScreen, transitionStyle: .coverVertical) {
Text("OK")
}
}) {
Text("Present me!")
}
}
}


通过这种方式,您可以根据需要更改演示文稿和过渡风格

关于ios - 如何在 swiftUI 中呈现 crossDissolve View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60391281/

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