1

I have UIWebView that automatically resizes to the users orientation when they rotate the device with the following code:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

the code works fine if the user rotates while looking at the webView, the problem occurs if the user rotates the device in a different view(previous screen) and then navigates to my webView (screen), my webview becomes cut off. Im not sure how to automatically rotate it based on detected orientation. I tried putting the following code into my viewDidLoad but it doesnt seem to work

[self.webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

any suggestions would be greatly appreciated.

Mike
  • 122
  • 7

2 Answers2

2

I have found that the UIWebView does the wrong thing if it starts out with the page rendered as Landscape and switches to Portrait. If you start out Portrait, and then go to landscape and then back to portrait it works? (It does in my version).

Since you seem to be filling your entire width of the device with the UIWebView, you should be able to fix this with HTML. If this doesn't work, then you can set the viewport in JavaScript and also set it inside your application to the correct Frame size. But, by adding the following inside the <head> tag the page switch correctly from Landscape to Portrait:

   <meta name="viewport" content="width=device-width" />

You can also set the initial scale or min/max, but setting the width should be enough. I had thought this was the default, but it seems like the UIWebView has something where it caches the initial rendered width. You will still need to have the autoresizing code in order for the UIWebView to change the native size.

christophercotton
  • 5,829
  • 2
  • 34
  • 49
  • worked like a charm thanks : – Mike Jul 29 '11 at 19:18
  • can i have the actual javascript for changing this?? – Izac Mac Jun 06 '12 at 10:03
  • Sure, it is formatted correctly in another question: http://stackoverflow.com/questions/6007904/uiwebview-donest-resize-correctly-when-orientation-change/6104537#6104537 but here it is: [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", (int)webView.frame.size.width]]; – christophercotton Jun 06 '12 at 20:45
0

Sometimes, you may have to use the method

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
    toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    webview.frame=CGRectMake(0, 0, 480, 320);
}
else
{
    webview.frame=CGRectMake(0, 0, 320, 480);
}
}

And check whether the autoresizingMask property for both self.view and the webview is set to YES.

jeevangs
  • 306
  • 5
  • 20
  • @Mike: let me know, whether this helped you or not. – jeevangs Apr 16 '11 at 22:49
  • Hi Jeevangs, I tried the code you provided, was not successful however your advice with regards to autoresizingMask pointed me into the right direction >> i implemented this code into viewDidLoad: 'webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;' and it works now. But the issue that i have now is for example: when im in the landscape mode and i navigate to webView its now correctly resized into landscape mode, but if i rotate into portrait it still remains in landscape mode here is an image: [link]http://postimage.org/image/uomxfdz8/ – Mike Apr 17 '11 at 00:35