Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView changes contentSize on iOS 7 after coming back from background

The Problem:

I have a UIWebView between my UINavigationBar and UITabBar. On iOS 7, when the app enters background and comes back again, it seems as if the contentSize property of the UIWebView changes so that there is a white block above it.

After monitoring the contentSize of the UIWebView, when the application enters foreground (I check it by observing the UIApplicationDidBecomeActiveNotification), it has 49 substracted from it's height.

Screenshot (redacted sensitive info): Redacted version of the screenshot

My Settings:

I am using a UIWebView in my UIViewController that is embedded in a UITabBarViewController that is embedded in a UINavigationController. I am using Storyboards and having "Adjust Scroll View Insets", "Resize View From NIB" and "Extend Edges Under Tob Bars" on. On the UIWebView I have unchecked "Scales Page To Fit". I am not doing anything in the viewDidAppear or viewWillAppear methods. I tested it on iOS 6, and the problem doesn't occur there. Also, I am not using auto-layout.

Relevant Code:

self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.bounces = NO;
self.webView.scalesPageToFit = NO;

After monitoring contentSize & frame in viewDidAppear & appHasGoneInForeground:

Frame goes from 320x455 to 320x455 contentSize goes from 320x504 to 320x455

Does anyone have an idea what I am missing here and/or why this happens? Thanks for your time!

like image 498
Thermometer Avatar asked Nov 28 '13 10:11

Thermometer


6 Answers

Please try self.automaticallyAdjustsScrollViewInsets = NO in webView's viewController.

automaticallyAdjustsScrollViewInsets is new property in iOS7 SDK, and default value is YES.It can auto adjust viewController's scrollView in iOS7.You can get more information with google search.

I hope it can help you.

like image 58
mengxiangjian Avatar answered Nov 13 '22 20:11

mengxiangjian


You can setContentSize or frame on:

-(void)viewDidLayoutSubviews

set UIWebview in xib bottomscle

Autorising

other wise you can set

self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
like image 39
payal Avatar answered Nov 13 '22 20:11

payal


This is very strange behavior. Are you using AutoLayout? If you are, double check that your constraints ensure that the webView's edges are always equal to the ViewController's view edges.

If you aren't using AutoLayout, set the autoresizingMask such that the leading and trailing space are fixed:

webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

If this doesn't work, you can add a hack/workaround using KVO (please only use this as a last resort, because it's not good practice):

[view addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

Then implement the method

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

and set the contentSize back to what it should be.

like image 42
paulrehkugler Avatar answered Nov 13 '22 18:11

paulrehkugler


"I am using a UIWebView in my UIViewController that is embedded in a UITabBarViewController that is embedded in a UINavigationController".

This is not supported by Apple. link

...the order of containment is important; only certain arrangements are valid. The order of containment, from child to parent, is as follows:

Content view controllers, and container view controllers that have flexible bounds (such as the page view controller)

Navigation view controller

Tab bar controller

Split view controller

I would try a simpler and valid container arrangement first to and see if it resolves the issue. Note that even if you solve this specific issue, if you keep using your hierarchy you may still run into unexpected and possibly inconsistent UI issues.

like image 41
Danra Avatar answered Nov 13 '22 20:11

Danra


You can try this code (i know, that it's unsafe)

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

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

   }
 }

The number after the _webView.frame.origin.y

like image 1
Melih Büyükbayram Avatar answered Nov 13 '22 18:11

Melih Büyükbayram


Hi mate please see image that would help you.

When you have the UIWebview or tableview please check the all the direction of auto resizing on the right of the UIwebview ( Size Inspector tab ) enter image description here

And also you have to mention the Delta Y to -20 and Delta Height to 20 which is for iOS 7

like image 1
Raviraj Jadeja Avatar answered Nov 13 '22 20:11

Raviraj Jadeja