2016-09-07 26 views
0

我已經在顯示PDF的ios中實現了一個簡單的webview,並且我想要做兩件事: 1-當在webview上顯示時,刪除PDF文檔周圍的灰色填充區域 2 - 在給定按鈕座標的pdf文件上的特定位置顯示按鈕UIWebView webViewDidStartLoad方法沒有在swift中調用

對於上述兩個問題,我已經找到了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

爲什麼你的函數webViewDidStartLoad在viewDidLoad方法裏面?你沒有得到任何編譯錯誤? – jo3birdtalk

+0

感謝您的回覆。不,沒有錯誤。它應該在哪裏? @ jo3birdtalk – Nilo0f4r

回答

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 

    } 
+0

謝謝!這是正確的答案。 – Nilo0f4r

相關問題