2012-03-18 18 views
2

我正在開發一個應用程序,在其中使用加載了url的UIWebView。我想隱藏導航欄(這是第一個div),所以我實現工作正常的情況如下:UIWebView注入在webViewDidStartLoad中隱藏導航欄

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
// finished loading, hide the activity indicator in the status bar 
[self activityIndicatorAnimate:NO]; 
[self.browser stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');" 
"script.type = 'text/javascript';" 
"script.text = \"function myFunction() { " 
"var b=document.getElementsByTagName('div')[0];b.style.visibility='hidden';" 
"}\";" 
"document.getElementsByTagName('head')[0].appendChild(script);"]; 
[self.browser stringByEvaluatingJavaScriptFromString:@"myFunction();"]; 

NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString: @"document.all[0].innerHTML"]); 

} 

事實是,這得到當文件被加載執行,因爲它需要時間要下載的網址,加載時導航欄仍然可見。無論如何,我可以讓它從一開始就隱藏導航欄嗎?

注意:如果我在webViewDidStartLoad中添加此代碼,它不會執行,因爲元素尚未加載。

在此先感謝

回答

1

我不知道如何將內容加載在之前隱藏它,但我想替代一個 - 你可以加載本地HTML的網頁視圖,然後通過使用Ajax加載頁面,在ajax的響應中,用html做一些事情,然後在你的webview中顯示它。要加載本地文件到您的WebView,檢查這些代碼 -

NSString *html = [NSString alloc]; 

html = @"index.htm"; 
NSBundle *bundle = [NSBundle mainBundle]; 
NSString *path = [bundle bundlePath]; 

NSString *fullPath = [NSBundle pathForResource:html ofType:nil inDirectory:path]; 

NSURL *url = [NSURL fileURLWithPath:fullPath]; 
url = [url URLByDeletingLastPathComponent]; 

url = [NSURL URLWithString:@"index.htm?page=1" relativeToURL:url]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 


[self.mHtmlViewer loadRequest:request]; 

記住你的HTML通過拖放它們添加到您的項目到項目中。