Umeumeume
Umeumeume

Reputation: 1978

How to add padding on UIWebview using Javascript

I want to add padding on UIWebview using Javascript, because I switch the view between fullscreen and navigation bar mode, and navigation bar hides the top side of original pages.

This is the code. It works on some pages, but doesn't work on some pages. Does anybody have some ideas to make it work on all pages?

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

It works fine on this page. (Left pic) http://pandodaily.com/2012/03/23/jessica-albas-the-honest-company-raises-27-million-for-non-toxic-baby-products/

It doesn't work on this page. (Right pic) 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

Upvotes: 3

Views: 5132

Answers (1)

Umeumeume
Umeumeume

Reputation: 1978

My friend told me the better solution, so I want to share it.

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

Upvotes: 9

Related Questions