UIWebView rotation on iPad

2019-03-18 13:51发布

On the iPad, I present a view in the "detail" side of a split view controller that is basically just a UIWebView, which loads an HTML file in the application bundle. The application supports rotation and hides/shows the "master" side of the split as appropriate.

When the UIWebView is initially loaded in landscape mode, its content seems to be "sized" properly... the content is taller than the screen, so you can scroll vertically, but not horizontally. (The HTML content is nearly all text styled with CSS, with only a small ~300x50 image at the bottom.)

If you then rotate the screen to portrait, the HTML content still seems to be sized okay -- vertical scrollbar is present, but not horizontal, as it was initially. Rotating back to landscape and everything is still peachy.

So far, so good.

Now, if the UIWebView initially loads in portrait orientation, everything is also "sized" properly (vertical scrollbar, no horizontal). But, if you rotate it to landscape, the content suddenly gets a horizontal scrollbar, because one of the paragraphs of text is wider than the width of the UIWebView. (Not coincidentally, I'm sure, but that long paragraph is sized perfectly for the slightly larger width the UIWebView has when it's oriented as portrait.)

I was expecting/assuming that rotation of the iPad would cause the UIWebView to have its frame be resized, and when it's resized, to also resize its HTML content appropriately. (Think of taking a Web browser window and shrinking it.) Why isn't that happening for me?

I would like to avoid using the "Scales Pages to Fit" property because the text shrinks non-deterministically.

2条回答
在下西门庆
2楼-- · 2019-03-18 14:35

Just solved my issue with this by adding this HTML5 Viewport meta tag:

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

to the head section of my HTML, maybe a variation of this meta tag may help?

Although my issue was triggered when my app was started in landscape mode, while yours in portrait mode, it may be a common cause.

I also had another funny issue with the webView in iPad and the common solution seems to be that meta tag:

iPad Simulator WebView/Google Maps API Issue

查看更多
The star\"
3楼-- · 2019-03-18 14:36

It's also possible to deal with this by putting

[myWebView reload];

into the shouldAutorotateToInterfaceOrientation method

-- but if you're on a 3G connection this can be slow

查看更多
登录 后发表回答