Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

objective c - Rendering PDF in UIWebView iOS 8, causes a black border around PDF

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];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After a bit of investigating the issue I managed to understand where the problem is. So the UIWebView is an UIView, with an UIScrollView (with private class _UIWebViewScrollView) as subview. The loading of a PDF into the UIWebView goes under the following flow:
1) UIWebView starts loading the request. By this time -webViewDidStartLoad: delegate method is called.
2) After the PDF is (down)loaded the delegate method -webViewDidFinishLoad: is called. By that time the UIWebView knows this is a PDF file and a subview with a private class UIWebPDFView is already inserted into the _UIWebViewScrollView, but the PDF itself is not rendered, yet.
And here comes the problem.
3) The PDF is rendered offscreen and after it's ready a new subview with private class UIPDFPageView is inserted into UIWebPDFView and the PDF is displayed. The problem is when this insertion happens the UIWebPDFView has its backgroundColor set to black and this insertion happens after the -webViewDidFinishLoad: is called (the time depends on how big the PDF is to render). That's why it's not a good solution to go through all subviews of the UIWebView and set their backgroundColor property to white, for example.

The good news is that the UIViewController's method -viewDidLayoutSubviews is called when the UIPDFPageView is inserted into the UIWebView's view hierarchy. So, in the end the solution is to have this objective-C code into our view controller:

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

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

And in Swift:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    var v:UIView? = self.webView
    while (v != nil) {
        v!.backgroundColor = UIColor.whiteColor()
        v = v!.subviews.first
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...