我在ios中实现了一个简单的webview,显示了一个PDF,我想做两件事: 1-如果在webview上显示,请删除pdf文档周围的灰色填充区域 在给定按钮坐标的情况下,在pdf文件上的特定位置显示一个按钮
对于上述两个问题,我在SO上找到了代码并将其更改为我的需求。这是我到目前为止的代码(视图控制器)
class DocAreaController: UIViewController, UIWebViewDelegate{
@IBOutlet var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
myWebView.scrollView.scrollEnabled = false
myWebView.scrollView.bounces = false
myWebView.scrollView.pagingEnabled = true
myWebView.userInteractionEnabled = true
myWebView.delegate = self
func webViewDidStartLoad(myWebView: UIWebView){
let paddingScript = "document.body.style.margin='0';document.body.style.padding = '0'"
let result = myWebView.stringByEvaluatingJavaScriptFromString(paddingScript)
debugPrint("method called")
let btn: UIButton = UIButton(frame: CGRectMake(188, 340, 46, 30))
btn.backgroundColor = UIColor.cyanColor() //.colorWithAlphaComponent(0.5)
btn.userInteractionEnabled = false
btn.tag = 1 // change tag property
myWebView.addSubview(btn) // add to view as subview
}
// Get the document's file path.
let filepath = (NSBundle.mainBundle().pathForResource("Reader", ofType: "pdf"))! as String
// Create an NSURL object based on the file path.
let url = NSURL.fileURLWithPath(filepath)
// Create an NSURLRequest object.
let request = NSURLRequest(URL: url)
// Load the web viewer using the request object.
myWebView.loadRequest(request)
}
}
但是没有调用webViewDidStartLoad方法。我已经在代码和故事板中设置了webview的委托。我错过了什么?
答案 0 :(得分:0)
将webViewDidStartLoad移出viewDidLoad方法。将它放在viewDidLoad之后,它应该可以工作。
override func viewDidLoad() {
super.viewDidLoad()
myWebView.scrollView.scrollEnabled = false
myWebView.scrollView.bounces = false
myWebView.scrollView.pagingEnabled = true
myWebView.userInteractionEnabled = true
myWebView.delegate = self
// Get the document's file path.
let filepath = (NSBundle.mainBundle().pathForResource("Reader", ofType: "pdf"))! as String
// Create an NSURL object based on the file path.
let url = NSURL.fileURLWithPath(filepath)
// Create an NSURLRequest object.
let request = NSURLRequest(URL: url)
// Load the web viewer using the request object.
myWebView.loadRequest(request)
}
func webViewDidStartLoad(myWebView: UIWebView){
let paddingScript = "document.body.style.margin='0';document.body.style.padding = '0'"
let result = myWebView.stringByEvaluatingJavaScriptFromString(paddingScript)
debugPrint("method called")
let btn: UIButton = UIButton(frame: CGRectMake(188, 340, 46, 30))
btn.backgroundColor = UIColor.cyanColor() //.colorWithAlphaComponent(0.5)
btn.userInteractionEnabled = false
btn.tag = 1 // change tag property
myWebView.addSubview(btn) // add to view as subview
}