WKWebView shows a cropped background and blank space after rotation on iPad
This is a bug in WKWebKit that's usually triggered by HTML elements with position: fixed
applied. The only workaround I've found is to use window.scrollTo()
to scroll the page by one pixel a few hundred miliseconds after a resize
event.
WKWebView shows pdf inside a border, I don't want the border
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()
Load WKWebView in background/off screen
You can add the WKWebView to the view hierarchy but its x is your current width, so it lays out of the screen but within the rendering hierarchy.
Add WKNavigationDelegate to your class and add it to the webView like
webView.navigationDelegate = self
Then implement the following function:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation)
This function and some others are called when the webview finished loading the content, BUT this does not include the 100% finished rendering. After that there is still some time required for rendering, the time consumption for this depends on the content that was loaded.
There currently is no callback for the 100% finished loading and rendering, so if you know the files, you can calculate or use a fix delay before moving the webView into the visible rect.
OR
If you feel fine with that, you observe private values in the webview and move your webview after those value changes to your preferred state. This looks for example like that:
class MyCustomWKWebView: WKWebView {
func setupWebView(link: String) {
let url = NSURL(string: link)
let request = NSURLRequest(url: url! as URL)
load(request as URLRequest)
addObserver(self, forKeyPath: "loading", options: .new, context: nil)
}
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let _ = object as? WKWebView else { return }
guard let keyPath = keyPath else { return }
guard let change = change else { return }
switch keyPath {
case "loading":
if let val = change[NSKeyValueChangeKey.newKey] as? Bool {
//do something!
}
default:
break
}
}
deinit {
removeObserver(self, forKeyPath: "loading")
}
}
Related Topics
Swift 3 - Passing Data Between a View Controller and After That to Another 2
iOS Swift App with More Photos: Performance and Storage Suggestions
Swift: Nil Is Incompatible with Return Type String
iOS Sound Not Playing in Swift
How to Turn Off Core Data Write-Ahead Logging in Swift Using Options Dictionary
iOS Swift Avplayer Inside Uiview How to Make It Work
How to Go Back to Rootviewcontroller from Presentview Controller
How to Get All Events Out of a Calendar (Swift)
How to Use Tap Gesture in Accessibility in Swift
In Swift, for Anyobject, How to Setvalue() Then Call Valueforkey()
iOS Development App Startup Crash
Uitextfield Starting Cursor Position Is Wrong
Swift Good Coding Practice: If Statement with Optional Type Bool
Swift - Nsdate and Last Week of Year
How to Set the Size of an Uiviewrepresentable