如何使用Javascript在UIWebview上添加填充

时间:2012-03-25 13:10:46

标签: javascript iphone css 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

1 个答案:

答案 0 :(得分:9)

我的朋友告诉我更好的解决方案,所以我想分享一下。

- (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);
}