2012-03-25 53 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); 
}