ios - 如何在 swiftUI 中呈现 crossDissolve 视图?
问题描述
在 UIKit 中,我使用下面的代码来呈现具有演示样式crossDissolve的模态 Viewcontroller
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!")
}
}
}
通过这种方式,您可以根据需要更改演示文稿和过渡风格
推荐阅读
- scala - countWindow 和 timeWindow 如何连接?
- php - 索引为 google.com 的网站和所有页面均被排除在外
- .htaccess - 从模块博客到信息页面的 Opencart URL 重定向
- mysql - 如何在日期时间中减去 1 天?
- firebase - 我可以在 Firebase 中使用哪些软件包版本?
- c# - Xamarin - Is there a similar framework to Photokit (iOS) available for Android, or a good way to get the filestream of all images in the gallery?
- python-3.x - 概念问题:序列 - 有序 - 可单独访问的元素容器 [k]
- php - PHP:设置公共属性时调用函数
- sql - Doctrine 抛出错误:预期的 Doctrine\ORM\Query\Lexer::T_FROM, got end of string." 错误
- javascript - 如何显示来自另一个网站的元素?