Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
WKWebView shows pdf inside a border, I don't want the border

I have a custom view that adds a WKWebView like this:

 override func awakeFromNib() {
    super.awakeFromNib()
    loadWebView()
}

func loadWebView() {
    pdfWebView = WKWebView(frame: .zero)

    pdfWebView.navigationDelegate = self
    pdfWebView.translatesAutoresizingMaskIntoConstraints = false
    pdfWebView.isHidden = true
    self.addSubview(pdfWebView)
    ... add constrains to self anchors - top, left, right, bottom
}

When loads finishes, I have this:

  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: {
        SVProgressHUD.dismiss()
        webView.isHidden = false
        webView.isOpaque = true
        webView.adjustInsets()
    })
}

I had to adjust the insets, because on iPhone X, sometimes the pdf sticked to the bottom, instead to the top.

extension WKWebView {
    func adjustInsets() {
        parentViewController?.automaticallyAdjustsScrollViewInsets = false
        scrollView.contentInset = .zero
        scrollView.scrollIndicatorInsets = .zero
        scrollView.contentOffset = .zero
    }
}

The result is this:

There is a gray border.

full border

I have tried this:

  1. setting view's, pdfWebView and pdfWebView.scrollView background with .white or .clear

  2. webView.isOpaque = false ( this is the single one that removes the bottom part of the gray - but not the border)

enter image description here

  1. I have used an extension to set the .clear color to all child views

    extension UIView {
        func clearBackgrounds() {
            self.backgroundColor = UIColor.clear
            for subview in self.subviews {
                subview.clearBackgrounds()
            }
        }
    } 
    

Nothing works to remove the border. I can not edit the pdf, because this one is downloaded from ws and used in multiple apps.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Solution : I found a trick to do this, I think you can use this with pdf that has white background. It has the idea, you can manipulate this with your design.

self.view.backgroundColor = UIColor.white
let urlString = "http://www.africau.edu/images/default/sample.pdf"
let string = "<html><body marginwidth=\"0\" marginheight=\"0\" style=\"background-color: transparent\"><embed width=\"100%\" height=\"100%\" name=\"plugin\" src=\"\(urlString)\" type=\"application/pdf\"></body></html>"
webView.loadHTMLString(string, baseURL: nil)

Edit : If you want to use this with base64 encoded pdf data, you can use with that :

var pdfBase64String = "" //Your pdf data
if let decodedData = Data(base64Encoded: pdfBase64String, options: .ignoreUnknownCharacters) {
        pdfWebView.load(decodedData, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: URL(fileURLWithPath: ""))
}

Old Answer : Turning off "WebKitDiskImageCacheEnabled" can be fix this problem. Can you try this before load the pdf ?

UserDefaults.standard.set(false, forKey: "WebKitDiskImageCacheEnabled")
UserDefaults.standard.synchronize()
over 4 years ago · Santiago Trujillo Report

0

I will use PDFKit for iOS 11 + in this custom view with the fallback to WKWebView for earlier versions. PDFView does not have any gray background.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!