ios - UIWebView webViewDidStartLoad 方法未在 swift 中调用

标签 ios swift webview

我在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/

相关文章:

ios - 如何将 UITableView 单元格设置为看起来像消息气泡?

c++ - QML 文本滚动

java - Android Webview,缩放内容以适合屏幕

ios - iPhone 应用程序 PayPal 集成

ios - Swift 3 : Find out, 如果日期已更改 - 怎么办?

swift - tvOS 背景音乐和播放/暂停按钮

ios - 为什么我的简单 swift 委托(delegate)和协议(protocol)设置不起作用?

iphone - 如何使委托(delegate)对象在整个 ViewController 中都可访问

Android 无法检测到 iOS 背景 BLE 广告

macos - Webview 确实完成了 HTML5 视频播放器未调用的框架的加载