开发者

Having a problem making UIWebView autoresize to users orientation

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:CGRe开发者_如何学GoctMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

any suggestions would be greatly appreciated.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜