UIWebview删除填充/余量
我正在加载PDF到UIWebview
,我改变了背景颜色清除并设置为不透明为false。但是现在我的UIWebView
中有一个填充或余量,我想删除,因此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)
}
但仍有填充或利润率,它看起来像这样:
我该如何解决这个问题?
为什么我需要这个固定的原因是因为我将要保存这一个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文档(纵向)的工作我的是景观,它工作正常。
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)
}
}
请让我不知道我知道,如果我做错什么
这也是我创建的唯一解决方案。有用 – Aximem
设置滚动视图帧到Web视图帧将没有任何效果,而网站的观点是在原点否则不想要的效果。
调整滚动视图contentInset
:
// set t,l,b,r to be negative CGFloat values that suit your content
webView.scrollView.contentInset = UIEdgeInsetsMake(t,l,b,r);
请举例? – user979331
这没有奏效,我尝试了以下方法:webView.scrollView.contentInset = UIEdgeInsetsMake(10,-15,-10,-150); – user979331
“没有工作”包含很少的信息。 – danh
ŧ他的例子给出了一个64的顶部空间,以允许导航栏。
let webView: UIWebView = UIWebView()
webView.frame = self.view.bounds
webView.scrollView.frame = webView.frame
webView.scrollView.contentInset = UIEdgeInsetsMake(64,0,0,0)
它适用于我应用负面内容插页。这适用于iOS 11和10,在斯威夫特4:
webView.scrollView.contentInset = UIEdgeInsets(top: -8, left: -8, bottom: -8, right: -8)
是有可能,PDF内容本身的格式与插图?无论如何,不要混淆滚动视图框架。这没有任何意义,因为Web视图可能在其父项的原始位置,否则将会造成危害。我也猜测YES已经是scalesPageToFit的默认值。我可以提供的最佳构造理念是webView.scrollView.contentInset。 – danh
请问您能回答这个问题吗 – user979331