我有一个 UIWebView,仅覆盖屏幕右侧的大部分。
为此,我使用frame 属性指定纵向的框架大小和位置。
webView.frame=CGRectMake(220,100,548,875);
当设备旋转为横向时,我为横向指定一个新框架:
webView.frame=CGRectMake(300,73,724,638);
问题是,当我将 webView 从纵向旋转到横向,然后再次旋转到纵向,并将框架更改为原始框架时,其宽度只有之前的一半。
这是我的代码和调用来调整框架大小的函数:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
}
else if(self.interfaceOrientation==UIInterfaceOrientationPortrait)
{
[self showLandscape];
}
else if(toInterfaceOrientation==UIInterfaceOrientationPortrait)
{
[self showPortrait];
}
}
-(void)showLandscape
{
.
.
.
webView.frame=CGRectMake(300,73,724,638);
.
.
.
}
-(void)showPortrait
{
.
.
.
webView.frame=CGRectMake(220,100,548,875);
.
.
.
}
如何解决这个问题?
最佳答案
这就是我所做的,效果很好:
- (void)viewDidLoad {
UIWebView *_webView = [[UIWebView alloc] init];
// when I add these line it is also working well (!!!) on my device
//_webView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
//_webView.autoresizesSubviews=YES;
//_webView.contentMode=UIViewContentModeScaleAspectFit;
[self.view addSubview:_webView];
[_webView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)];
}
我还添加了以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
for (UIView *_temporaryView in self.view.subviews) {
if ([_temporaryView isKindOfClass:[UIWebView class]]) {
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
[_temporaryView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)];
} else {
[_temporaryView setFrame:CGRectMake(300.f, 73.f, 724.f, 638.f)];
}
}
}
return true;
}
如果您不想提供代码片段来查找异常,请将其与您的代码进行比较并找出差异。
关于ios - 旋转时 UIWebView 大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11645424/