UIWebView not autoresizing to fit screen
I have an app that uses a UIViewController to display articles in a UIWebView, 开发者_开发问答some of which have images. I have it set up so that when an image is clicked it calls
- (void)displayImage:(NSInteger)i {
ImageViewController * imageVC = [[ImageViewController alloc] initWithImageId:i];
[self.navigationController pushViewController:imageVC animated:YES];
[imageVC release];
}
This loads another UIViewController. All of my views responds perfectly fine to rotations. The ImageViewController maintains the proportions of the image and my ArticleViewController fills the screen with text. However, whenever I rotate the screen to landscape mode while viewing my ImageViewController and press the back button. The previous UIViewController autoresizes incorrectly, increasing the font size of the text rather than putting more words on a line.
EDIT:
This is an article view controller:
This is the ImageViewController that comes up when you click the image:
Now if we change the orientation here...
And hit the back button, the article content has been inappropriately resized to fit the window.
This can be corrected by turning the iPhone twice. This is what the article should look like in landscape.
The problem has something to do with the UIWebView not autoresizing appropriately when it isn't currently visible. What can I do to fix this?
You need to do this in Interface Builder.
Click on your UIWebView and press Apple-3 to pull up the "Size Inspector".
Under "Autosizing", make sure the two arrows inside the box are selected. This will make sure the view size is always maximized to its container.
You can change it on server side by adding different css styles depending on device orientation: for example:
/* Landscape */
@media screen and (min-width: 321px)
{
body{
width: 320px;
}
}
/* Landscape */
@media screen and (min-width: 321px)
{
body{
width: 480px;
}
}
or you could change viewport from client side: In case your page has got viewport meta tag:
<meta name="viewport" id="view" content="width=320px, minimum-scale=1.0, maximum-scale=1.0">
then in your code you can change this viewport every time orientation changes:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIDeviceOrientationLandscapeLeft)
[uiwebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"view\").setAttribute('content', 'width=480');"]];
}
....
and change it again when orientation is portrait
For future reference. You can do this programmatically with this.
[webView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
精彩评论