SwiftUI - PDFkit - change PDF viewer background color?

Viewed 175

I'm building an app that displays PDF files with SwiftUI and PDFkit. Around the PDF file is the color gray with light theme and black with dark theme. Is it possible to change the background color from secondary to another color or image? If yes, how would I do that?

This is my code:

import SwiftUI
import PDFKit

struct MapPDFView: View {
    var url: URL
    
    var body: some View {
        ZStack {
        Image("Transparant")
            .resizable()
            .ignoresSafeArea()
            MapPDFView1(url)
        }
    }
}

struct MapPDFView1: UIViewRepresentable {
    let url: URL
    init(_ url: URL) {
        self.url = url
    }

    func makeUIView(context: UIViewRepresentableContext<MapPDFView1>) -> MapPDFView1.UIViewType {
        let pdfView = PDFView()
        pdfView.document = PDFDocument(url: self.url)
        pdfView.autoScales = true
        return pdfView
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<MapPDFView1>) {
        
    }
}

struct MapPDFView_Previews: PreviewProvider {
    static var previews: some View {
        MapPDFView(url: Bundle.main.url(forResource: "Map", withExtension: "pdf")!
        );}
}

1 Answers

The PDFView is a UIView, so just set background color to which ever needed, like

func makeUIView(context: UIViewRepresentableContext<MapPDFView1>) -> MapPDFView1.UIViewType {
    let pdfView = PDFView()
    pdfView.backgroundColor = .clear     // << here !!
Related