When used UIViewControllerRepresentable in SwiftUI, the UIHostingController was clip after navigation link

121 Views Asked by At

I try to use UIHostingController in UIViewControllerRepresentable inside SwiftUI NavigationView.

But I noticed a problem with this on iOS 16 the content in HostinController clipped after being moved to another view in NavigationLink.

Gif example

Code:

import SwiftUI

struct ContentView: View {

    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    TosterView { Color.green }
                        .frame(height: 170)

                    NavigationLink(destination: { Text("Destination")}, label: { Text("Go to destination")})

                    Color.orange.frame(height: 900)
                }
            }
        }
    }
}

public struct TosterView<PageContent: View>: UIViewControllerRepresentable {

    let contentForData: () -> PageContent

    public func makeUIViewController(context: Context) -> UIHostingController<PageContent> {
        UIHostingController(rootView: contentForData())
    }

    public func updateUIViewController(_ uiViewController: UIHostingController<PageContent>, context: Context) {}
}

P.S. Please don't recommend to use UIViewRepresentable or use ViewController with background Green

0

There are 0 best solutions below