Code: Select all
Image("picsum-237-1900x400")
.resizable()
.scaledToFill()
.frame(height: 200)
.frame(maxWidth: .infinity)
Code: Select all
struct NavigationWrapper: UIViewControllerRepresentable {
@ViewBuilder let rootView: () -> Content
func makeUIViewController(context: Context) -> UINavigationController {
let navController = UINavigationController()
let hostingController = UIHostingController(
rootView: rootView().environment(\.navigationController, navController)
)
navController.pushViewController(hostingController, animated: false)
return navController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
}
< /code>
Verwendung: < /p>
@main
struct MainApp: App {
var body: some Scene {
WindowGroup {
NavigationWrapper(rootView: { ProductView() })
}
}
}
struct ProductView: View {
@Environment(\.navigationController) private var navigationController
var body: some View {
VStack {
Button("Go to Product Details") {
let hostingVC = UIHostingController(rootView: ProductDetailsScreen())
navigationController?.pushViewController(hostingVC, animated: true)
}
}
.navigationTitle("Products")
}
}
struct ProductDetailsScreen: View {
var body: some View {
VStack {
Image("picsum-237-1900x400")
.resizable()
.scaledToFill()
.frame(height: 200)
.frame(maxWidth: .infinity)
Spacer()
Text("Product Details")
Spacer()
}
.navigationTitle("Details")
}
}
struct NavigationControllerKey: EnvironmentKey {
static let defaultValue: UINavigationController? = nil
}
extension EnvironmentValues {
var navigationController: UINavigationController? {
get { self[NavigationControllerKey.self] }
set { self[NavigationControllerKey.self] = newValue }
}
}
Gibt es einige Konzepte, die ich an der Überbrückung von Swiftui fehlt, und uikit, oder ist das ein Fehler in Swiftui und gibt es eine Problemumgehung dafür?