2012-03-25 51 views
3

我想使用Javascript在UIWebview上添加填充,因爲我在全屏和導航欄模式之間切換視圖,並且導航欄隱藏了原始頁面的頂部。如何使用Javascript在UIWebview上添加填充

這是代碼。它適用於某些頁面,但不適用於某些頁面。有沒有人有一些想法,使其在所有網頁上工作?

- (void)webViewDidFinishLoad:(UIWebView*)webView { 
    //make padding on the top and the bottom of webview 
    NSString *padding = @"document.body.style.padding='64px 0px 44px 0px';"; 
    [webView_ stringByEvaluatingJavaScriptFromString:padding]; 
} 

它在此頁面正常工作。 (左圖)http://pandodaily.com/2012/03/23/jessica-albas-the-honest-company-raises-27-million-for-non-toxic-baby-products/

它不起作用在此頁面上。 (右圖)http://www.washingtonpost.com/german-entrepreneur-makes-millions-in-eu-through-parallel-pharmaceutical-trade/2012/03/19/gIQAPnZrYS_story.html?wprss=rss_homepage

enter image description here enter image description here

回答

8

我的朋友告訴我更好的解決方案,所以我想分享。

- (BOOL)iOS5OrHigher { 
    NSString *iOSversion = [[UIDevice currentDevice] systemVersion]; 
    if ([iOSversion compare:@"5.0" options:NSNumericSearch] != NSOrderedAscending) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

if ([self iOS5OrHigher]) { 
    webView_.scrollView.contentInset = UIEdgeInsetsMake(44.0,0.0,44.0,0.0); 
} else { 
    UIScrollView *scrollview = (UIScrollView *)[webView_.subviews objectAtIndex:0]; 
    scrollview.contentInset  = UIEdgeInsetsMake(44.0,0.0,44.0,0.0); 
}