UIWebview移除填充/边距

8

我正在将一个PDF加载到UIWebview中,我将背景颜色更改为透明并将不透明设置为false。然而现在我的UIWebView中有一些填充或边距,我想要移除这些内容使得UIWebview填满整个屏幕。

我像这样创建了一个UIWebview:

let webview = UIWebView()
webview.frame = self.view.bounds
webview.scrollView.frame = webview.frame
webview.userInteractionEnabled = true
webview.scalesPageToFit = true
webview.becomeFirstResponder()
webview.delegate = self
webview.scrollView.delegate = self
webview.opaque = false
webview.backgroundColor = UIColor.clearColor()
self.view.addSubview(webview)
webview.loadRequest(NSURLRequest(URL:url))
webview.gestureRecognizers = [pinchRecognizer, panRecognizer]

我将其应用于webViewDidFinishLoad方法。
func webViewDidFinishLoad(webView: UIWebView) {
        let padding = "document.body.style.margin='0';document.body.style.padding = '0'"
        webView.stringByEvaluatingJavaScriptFromString(padding)
}

但是仍然存在填充或边距,效果如下:

enter image description here

我该如何解决这个问题?

我需要解决这个问题的原因是因为我要将此UIWebView保存为PDF文件,当我保存它时,那些额外的间距也会出现。以下是我的PDF生成代码:

func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {

        let data = NSMutableData()


        UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)


        UIGraphicsBeginPDFPage()


        printPageRenderer.drawPageAtIndex(0, inRect: UIGraphicsGetPDFContextBounds())


        UIGraphicsEndPDFContext()


        return data
    }

以及更多

let screenHeight = appDelegate.webview!.scrollView.bounds.size.height

        let heightStr = appDelegate.webview!.scrollView.bounds.size.height

        let height = heightStr

        let pages = ceil(height / screenHeight)

        let pdfMutableData = NSMutableData()

        UIGraphicsBeginPDFContextToData(pdfMutableData, appDelegate.webview!.scrollView.bounds, nil)

        for i in 0..<Int(pages)
        {

            if (CGFloat((i+1)) * screenHeight  > CGFloat(height)) {
                var f = appDelegate.webview!.scrollView.frame
                f.size.height -= ((CGFloat((i+1)) * screenHeight) - CGFloat(height));
                appDelegate.webview!.scrollView.frame = f
            }

            UIGraphicsBeginPDFPage()

            let currentContext = UIGraphicsGetCurrentContext()

            appDelegate.webview!.scrollView.setContentOffset(CGPointMake(0, screenHeight * CGFloat(i)), animated: false)

            appDelegate.webview!.scrollView.layer.renderInContext(currentContext!)

        }

        UIGraphicsEndPDFContext()

PDF内容本身是否带有插图格式?无论如何,请勿干扰滚动视图框架。这样做没有任何作用,因为Web视图可能已经在其父级的原点,否则会有害。我猜测scalesPageToFit的默认值已经是YES了。我能提供的最好的建设性想法是webView.scrollView.contentInset。 - danh
你可以把这个放在回答里吗? - user979331
6个回答

5

将滚动视图框架设置为网页视图框架,当网页视图处于原点时不起作用,否则会产生不良影响。

调整滚动视图的contentInset

// set t,l,b,r to be negative CGFloat values that suit your content
webView.scrollView.contentInset = UIEdgeInsetsMake(t,l,b,r);

这个没有起作用,我尝试了以下代码:webView.scrollView.contentInset = UIEdgeInsetsMake(10,-15,-10,-150); - user979331
“Did not work” 包含的信息非常少。 - danh
1
it remains the same as before - user979331
内边距和外边距保持不变。 - user979331

4

这看起来可以解决我的问题,不确定它在其他PDF文档(竖向)上的工作效果如何。我的文档是横向的,但它也可以正常工作。

appDelegate.webview = UIWebView()

        appDelegate.webview!.frame = CGRect(x: 0 - 8, y: 0 - 8, width: self.view.bounds.size.width + 14, height: self.view.bounds.size.height + 14)

        appDelegate.webview!.scrollView.frame = CGRect(x: 0 - 8, y: 0 - 8, width: self.view.bounds.size.width + 14, height: self.view.bounds.size.height + 14)

然后保存PDF文件

func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String)
    {

        let pdfData = NSMutableData()

        UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0 + 8, y: 0 - 8, width: aView.bounds.size.width - 17, height: aView.bounds.size.height - 117), nil)

        UIGraphicsBeginPDFPage()

        guard let pdfContext = UIGraphicsGetCurrentContext() else { return }

        aView.layer.renderInContext(pdfContext)
        UIGraphicsEndPDFContext()

        if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
            let documentsFileName = documentDirectories + "/" + fileName
            debugPrint(documentsFileName)
            pdfData.writeToFile(documentsFileName, atomically: true)
        }
    }

如果我做错了什么,请告诉我。


3

我发现使用负的内容插入可以解决问题。这个方法适用于iOS 11和10,Swift 4:

webView.scrollView.contentInset = UIEdgeInsets(top: -8, left: -8, bottom: -8, right: -8)


1
我不确定您是否已经找到了答案,但是作为一个快速的解决方法,您可以给出预先调整好的约束条件。
比如说,如果您的填充空间从四个方向都是10像素,并且它始终是一个常量,那么解决方法是:
在webView周围给出-10像素的空间,并且不要忘记为您的webView添加一个父视图,没有-10像素的边缘约束,并标记该父视图。
clipsToBounds = true 
clipSubViews = true 

enter image description here


0
我不知道为什么我来这里这么多次,但谷歌从未将我引导到实际回答的问题。对于有同样问题的任何人,这是实际答案:https://dev59.com/GXE95IYBdhLWcg3wHqal#2442859

问题在于浏览器添加了默认边距,简单的解决方法是添加以下内容:

body { margin: 0; padding: 0; }

在我的情况下,我这样做了,但第一个元素是一个默认具有“margin-top”的<p>。最好添加使用CSS重置并明确地设置所有样式。 - Sam Soffes

0

这个示例给了一个顶部间距为64,以便为导航栏留出空间。

let webView: UIWebView = UIWebView()

webView.frame = self.view.bounds

webView.scrollView.frame = webView.frame

webView.scrollView.contentInset = UIEdgeInsetsMake(64,0,0,0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接