Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView - Does not resize content on rotation change?

So I have a UIWebView that displays static html content.

When I start the ViewController in portrait and switch to landscape, it resizes the content correctly.

But, when I start the page in landscape and switch to portrait, it doesn't resize my content, and scrolling is required in order to view all the content.

Is this a bug? Is there a solution to force resizing the content of a UIWebView?

like image 370
aryaxt Avatar asked Mar 06 '12 00:03

aryaxt


4 Answers

You have 2 options:
Add this into HEAD section of your html file:

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

Or call [myWebView reload] when orientation changes

like image 124
Sergey Kuryanov Avatar answered Oct 19 '22 10:10

Sergey Kuryanov


I had a similar problem. I solved it by executing javascript code to generate an event that updates the orientation of the page when the UIViewController finished rotation:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [webView stringByEvaluatingJavaScriptFromString:@"var e = document.createEvent('Events'); "
                                                    @"e.initEvent('orientationchange', true, false);"
                                                    @"document.dispatchEvent(e); "];
}
like image 37
Dia Kharrat Avatar answered Oct 19 '22 09:10

Dia Kharrat


I tried the other suggestions above but they were not working the my static webpage I had loaded. I tried adding this line into the header of my webpage

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

But it still didn't resize the page width after changing the orientation between landscape & portrait on my device. I then added this line to func viewDidLoad() and it worked perfectly. I'm running iOS 11.4

myWebView.autoresizingMask = UIViewAutoresizing.flexibleWidth
like image 3
Clay Avatar answered Oct 19 '22 11:10

Clay


This question is old, but this should solve it:

myWebView.scalesPageToFit = YES;

Alternately, if you're using the Interface Builder, you can tick the check box under Attributes Inspector that says 'Sales page to fit.'

Hope it helps.

like image 2
Vinod Vishwanath Avatar answered Oct 19 '22 11:10

Vinod Vishwanath