2017-09-05 27 views
0

我們使用的代碼下面的線移動到Web視圖:無法UIWebView中移動到指定的部分iOS11(新iPad)

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.location.hash=\'#-1\';"]]; 
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.location.hash=\'#%@\';", index]]; 

但不知何故,此代碼不能在iPad上才能正常工作。這裏是我如何初始化網絡視圖:

webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
[webView setDelegate:self]; 
[webView.scrollView setDelegate:self]; 
[webView setBackgroundColor:WHITECOLOR]; 
[webView setScalesPageToFit:YES]; 
[webView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view addSubview: webView]; 

有人可以請指導我如何調試它。

+0

在iOS11'location.hash'似乎被打破,但它是一個簡單的解決這個。 使用通常的組合絕對 – shc

回答

0

在iOS11中location.hash似乎被打破,但它是一個簡單的解決方案。 而不是使用通常的組合:

location.hash = ""; 
location.hash = _some_hash_string_ 

採用javascript如下:

var hashElement=document.getElementsByName(_some_hash_string_)[0]; 
if(hashElement) { 
    hashElement.scrollIntoView(); 
} else { 
    // if the element is not found - scroll to top 
    document.documentElement.scrollIntoView(); 
} 
+0

JavaScript代碼很好,但我不認爲這有助於querent:他們不能簡單地將一大塊js代碼寫入他們的objective-c。 – royalmurder

+0

當然,這可以幫助,原始問題適用於UIWebView,它有'stringByEvaluatingJavaScriptFromString:'方法,WKWebView有'evaluateJavaScript:completionHandler:' – shc

+0

適用於iPad。 +1 – Jeff