In iOS 8, when rendering a .PDF into a UIWebview
there is a black border and background around the PDF displayed (not the whole background view). Note this is not the UIWebview background which is set to:
myWebView.opaque = NO;
myWebView.backgroundColor = [UIColor clearColor];
This is not present in < iOS8, (no black bordering coloured background around the .PDF)
Anyone else experienced this who could shed some light on this?
Im loading my PDF into the Web view like so..
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.pdfData != nil && self.viewHasUnloaded == YES) {
self.viewHasUnloaded = NO;
[self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
}
}
This is railwayparade's implementation in Swift:
Turn off WebKitDiskImageCacheEnabled and the black border goes away:
In applicationDidFinishLaunchingWithOptions add the following lines:
The solution is here:
EDIT (link Broken)
How to remove black border from PDF/UIWebView in iOS 8It worked for me.
The trick is to change the background color after webView finish loading.
EDIT
For anyone using Xamarin, a variation of gravers solution worked once translated to C#.
Place the following in the UIViewController:
Changing the background color of the subviews of the UIWebView after the PDF finishes loading -- the essence of most of the answers above -- works for me. However, I've found that since iOS 5, webViewDidFinishLoad is called before the web view actually finishes loading, so I've been doing this to avoid various problems in my apps:
The misleading behavior of webViewDidFinishLoad might explain why this approach isn't working for some people.
Another consequence of this behavior is that webView.scrollView.contentSize is not yet set when webViewDidFinishLoad is called. I submitted this to Apple as bug 10259756 in October 2011, but they closed the bug report with a status of "Behaves correctly."
I assume it's a bit of a timing-thing because
UIWebPDFView
is added as subview not until the rendering has finished andviewDidLayoutSubviews
is called before that happens. And because there is no event like didFinshedRendering i used a timer to check whenUIWebPDFView
is added.The following is working for me.
Put a instance variable in the in the header file or your class:
Put these methods in the implementation file:
Now put this line right after the line where you load the pdf:
Hints:
SYSTEM_VERSION_LESS_THAN
is a macro to enshure the code is only execuded on iOS 8 and above.self.pdfWebView
is your UIWebView.