Question

Im loading local html files, since iOS7 there is added white space on top in the UIWebView.(I cant post an image as i do not have enough points.) image can be seen here- snap shot from iPhone simulator, uiwebview surrounded by black frame, the html content is grey, but there is white added above it

I have tried to adjust the zoom using

[webView stringByEvaluatingJavaScriptFromString:@"document. body.style.zoom = 5.0;"];
webView.scalesPageToFit = NO;

I also set tried to remove white spacing:

 NSString *padding = @"document.body.style.margin='0';document.body.style.padding = '0'";
[webView stringByEvaluatingJavaScriptFromString:padding];

still no luck. In the desktop chrome browser there is no whitespace. The html files are Google Swiffy files - containing html and JSON.

edit: updated Image

Was it helpful?

Solution

Try self.automaticallyAdjustsScrollViewInsets = NO; in ViewDidLoad.

ios 7 add 64px automatically for scroll view. (status bar and nav bar)

OTHER TIPS

This problem only affects the UIWebView if it is the first subview of the parent view. One alternative way to work around this problem is to add another non-visible empty view to the parent view as the first view. In Interface Builder add a zero size subview and use the Editor->Arrange->Send to Back menu command.

If you're not using Interface Builder, but instead are subclassing the UIWebView, then it can be done by creating a UIView instance variable called scrollFixView and overriding the following methods:

- (void)didMoveToSuperview
{
  [super didMoveToSuperview];

  if ([self superview].subviews.firstObject == self) {
    _scrollFixView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _scrollFixView.hidden = YES;
    [[self superview] insertSubview:_scrollFixView belowSubview:self];
  }
}

- (void)removeFromSuperview
{
  if (_scrollFixView) {
    [_scrollFixView removeFromSuperview];
    _scrollFixView = nil;
  }

  [super removeFromSuperview];
}

I had the same problem so I tried a few things:-)

This worked for me, but correct me please if there is a better way.

-(void)webViewDidFinishLoad:(UIWebView *)webView
 {
   if(self.navigationController.navigationBar.translucent == YES)
   {

    _webView.scrollView.contentOffset = CGPointMake(_webView.frame.origin.x, _webView.frame.origin.y - 54);

   }
 }

So basically you need to :

1) Add the UIWebView delegate method - webViewDidFinishLoad: 2) Then I setup an if statement to check if the translucent option is active.

The last one you only need to do of course if you give the user the option within your app. The number after the _webView.frame.origin.y is just for my app. It may differ for you.

I solved this problem by simply setting a constraint on the WebView, setting the top space between it and the View top to 0, causing the NavBar to overlap the whitespace.

One alternative to Jeff Kranenburg's method is to subclass and override the UIWebView subclasses' UIScrollViewDelegate method scrollViewDidScroll:. This is only appropriate if scrolling is turned off for your subclass.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if ([[self superclass] instancesRespondToSelector:_cmd]) {
    [super scrollViewDidScroll:scrollView];
  }

  [self fixUpScrollViewContentOffset];
}

- (void)fixUpScrollViewContentOffset
{
  if (!CGPointEqualToPoint(self.scrollView.contentOffset, CGPointZero)) {
    self.scrollView.contentOffset = CGPointZero;
  }
}

I already got it .

here my logic code, When the application open the website you must get the size of your webview then set it on height

here my code

     ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams();
            p.height = webpage.getHeight();
// check if how long you need to set your height for webpage then set it :)
            Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight()));
            webpage.setLayoutParams(p);

Hope you will take my code and my answer to :) works on any devices even tabs too :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top