'SwiftUI - View dismissing itself upon rotation

I have a view that displays images. When tapping on an image, it launches a full screen version where the user can pinch/zoom. However, when rotating the device from portrait to landscape in that view, the view dismisses itself. Oddly there are some images where the view will not dismiss itself and work properly. However, for most images the view automatically dismisses upon rotation. I should note that this happens whether it is presented as a full screen or sheet.

struct GalleryGridView: View {
    @ObservedObject var galleryVM:GalleryPublisher
    
    var body: some View {
        
        let columns = [
            GridItem(.flexible(minimum: 100)),
            GridItem(.flexible(minimum: 100)),
            GridItem(.flexible(minimum: 100))
        ]
        LazyVGrid(columns: columns) {
            ForEach(galleryVM.object.artworks ?? [], id:\.self) { artwork in
                ImageView(path: artwork.asset?.path)
                    .cornerRadius(10)
                    .id(artwork.id)
            }
        }
    }
    
    struct ImageView:View {
        var path:String?
        
        @State private var image:UIImage?
        @State private var gotoFullScreen = false
        
        var body: some View {
            ZStack {
                if let image = image {
                    Image(uiImage: image)
                        .resizable()
                        .aspectRatio(1, contentMode: .fill)
                        .onTapGesture {
                            gotoFullScreen = true
                        }
                } else {
                    Rectangle()
                    ProgressView()
                        .progressViewStyle(CircularProgressViewStyle(tint: .blue))
                }
            }
            .sheet(isPresented: $gotoFullScreen, content: {
                FullImageView(image: image!)
            })
            .onAppear {
                guard let path = path else {print("Missing image path");return}
                NetworkManager.shared.downloadImage(urlStr: path) { image in
                    self.image = image
                }
            }
        }
    }
}


struct FullImageView: View {
    var image:UIImage

    var body: some View {
        PhotoDetailView(image: image)
    }
}

struct PhotoDetailView: UIViewRepresentable {
    let image: UIImage

    func makeUIView(context: Context) -> PDFView {
        let view = PDFView()
        view.document = PDFDocument()
        guard let page = PDFPage(image: image) else {return view }
        view.document?.insert(page, at: 0)
        view.autoScales = true
        return view
    }

    func updateUIView(_ uiView: PDFView, context: Context) {}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source