我在ios中实现了一个简单的webview,并显示PDF,我想做两件事: 1-当 pdf 文档显示在 web View 上时,删除它周围的灰色填充区域 2-在给定按钮坐标的 pdf 文件上的特定位置显示按钮
对于上述两个问题,我在 SO 上找到了代码并将其更改为我的需要。这是我到目前为止的代码( View Controller )
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 方法没有被调用。我已经在代码和 Storyboard中设置了 webview 的委托(delegate)。我错过了什么?
最佳答案
将 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
}
关于ios - UIWebView webViewDidStartLoad 方法未在 swift 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39360028/