Rendering PDF in UIWebView iOS 8, causes a black b

2020-01-30 04:25发布

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];
    }
}

12条回答
兄弟一词,经得起流年.
2楼-- · 2020-01-30 05:00

This is railwayparade's implementation in Swift:

NSUserDefaults.standardUserDefaults().setObject(false, forKey: "WebKitDiskImageCacheEnabled")
NSUserDefaults.standardUserDefaults().synchronize()
查看更多
神经病院院长
3楼-- · 2020-01-30 05:03

Turn off WebKitDiskImageCacheEnabled and the black border goes away:

In applicationDidFinishLaunchingWithOptions add the following lines:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
查看更多
迷人小祖宗
4楼-- · 2020-01-30 05:07

The solution is here:

EDIT (link Broken)

How to remove black border from PDF/UIWebView in iOS 8

It worked for me.

The trick is to change the background color after webView finish loading.

EDIT

In iOS 8.0, if you are loading a PDF page in UIWebView then you will get the black border around the page.

When you load PDF in to webview then it insert UIWebPDFView into webview.

When PDF is rendered it add a new private class UIPDFPageView into UIWebPDFView which have a black background color. This all insertion happens after the -webViewDidFinishLoad: method called. So you need to set clear or white background color set after the -webViewDidFinishLoad: method

Add the following method for remove the black border color

Call the function in -webViewDidFinishLoad:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
    [self performSelector:@selector(clearBackground) withObject:nil afterDelay:0.1];
}

.

- (void)clearBackground {
    UIView *v = webVw;
    while (v) {
        //v.backgroundColor = [UIColor whiteColor];
        v = [v.subviews firstObject];

        if ([NSStringFromClass([v class]) isEqualToString:@"UIWebPDFView"]) {
            [v setBackgroundColor:[UIColor whiteColor]];

            // background set to white so fade view in and exit
            [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 webVw.alpha = 1.0;
                             }
                             completion:nil];
            return;
        }
    }
}
查看更多
混吃等死
5楼-- · 2020-01-30 05:07

For anyone using Xamarin, a variation of gravers solution worked once translated to C#.

Place the following in the UIViewController:

public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();

        var subViews = webView.Subviews;
        foreach (UIView v in subViews) {
            v.BackgroundColor = UIColor.Clear;

            var subv = v.Subviews;
            foreach (UIView w in subv) {
                w.BackgroundColor = UIColor.Clear;

            }
        }
    }
查看更多
我命由我不由天
6楼-- · 2020-01-30 05:08

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:

- (void)webViewDidFinishLoad:(UIWebView *)documentView {
    [self performSelector:@selector(webViewDidReallyFinishLoad:) withObject:documentView afterDelay:0.5];
}

- (void)webViewDidReallyFinishLoad:(UIWebView *)documentView {
    // now we can restore the transparency
}

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."

查看更多
Anthone
7楼-- · 2020-01-30 05:10

I assume it's a bit of a timing-thing because UIWebPDFView is added as subview not until the rendering has finished and viewDidLayoutSubviews is called before that happens. And because there is no event like didFinshedRendering i used a timer to check when UIWebPDFView is added.

The following is working for me.

Put a instance variable in the in the header file or your class:

// instance variable for interval timer
NSTimer *BackgroundIntervalTimer;

Put these methods in the implementation file:

- (void)startUIWebViewBackgroundFixTimer {
    // if > iOS 8 start fixing background color of UIWebPDFView
    if (!SYSTEM_VERSION_LESS_THAN(@"8.0")) {
        // hide pdfWebView until background fixed
        self.pdfWebView.alpha = 0.0;
        // start interval timer
        BackgroundIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:0.1  target:self selector:@selector(fixBackground) userInfo:nil repeats:YES];
    }
}

- (void)stopUIWebViewBackgroundFixTimer {
    [BackgroundIntervalTimer invalidate];
    BackgroundIntervalTimer = nil;
}

- (void)fixBackground {
    // stop timer interval
    [self stopUIWebViewBackgroundFixTimer];

    // Assuming self.webView is our UIWebView
    // We go though all sub views of the UIWebView and set their backgroundColor to white
    UIView *v = self.pdfWebView;
    while (v) {
        //v.backgroundColor = [UIColor whiteColor];
        v = [v.subviews firstObject];

        if ([NSStringFromClass([v class]) isEqualToString:@"UIWebPDFView"]) {
            [v setBackgroundColor:[UIColor whiteColor]];

            // background set to white so fade view in and exit
            [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 self.pdfWebView.alpha = 1.0;
                             }
                             completion:nil];
            return;
        }
    }
    // UIWebPDFView doesnt exist yet so exit and try later
    [self startUIWebViewBackgroundFixTimer];
}

Now put this line right after the line where you load the pdf:

// if iOS 8 check if pdfWebView got subview of class UIWebPDFView
[self startUIWebViewBackgroundFixTimer];

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.
查看更多
登录 后发表回答