ios - 旋转时 UIWebView 大小不正确

标签 ios webview frame

我有一个 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/

相关文章:

c++ - 如何重置 QML WebView?

ios - 可见层的 (CAShapeLayer) 框架为零 - Swift

ios - XMPPFramework - 登录 Openfire 服务器时为 "<not-authorized>"

iphone - iOS 应用程序被拒绝 - 数据存储指南

ios - 字符串 boundingRect 计算出的高度与 UITextView 内容高度不匹配

ios - 绘制 CIImage 的背景颜色

javascript - 在 Android Webview 中下载文件

android - DOM 存储(如果可用)

wpf - 创建的 Viewport2DVisual3D 框架不可见

delphi - XE2 Update 4 TFrame 与 TPageControl 与锚定控件的行为很奇怪