缩放操作+ UIInterfaceOrientation更改后,iPhone UIWebView宽度不适合

cho*_*eat 14 iphone zoom width uiwebview autoresize

我创建了一个带有UIWebView的裸骨iPhone应用程序(Scales Page to Fit = YES,shouldAutorotateToInterfaceOrientation = YES)并加载了一个网页,例如https://stackoverflow.com/

旋转设备显示UIWebView自动调整大小以适应宽度.好.

不正确:放大页面并缩小.现在旋转设备会在其中一个方向上以奇怪的宽度显示UIWebView(如果你放大横向,则纵向宽度很奇怪,反之亦然).仅当您导航到另一个页面时,此行为才会得到修复.

正确:在Mobile Safari中加载相同的URL.无论缩放练习如何,旋转工作和宽度都适合.

这是一个UIWebView错误(可能不是)?或者是否需要做一些事情来使事情像移动Safari一样"正常工作"?

M P*_*des 15

我找到了适合我的东西.问题是,当uiwebview更改其方向时,Web内容会缩放以适合视口.但是scrollview子视图的zoomscale参数没有正确更新(也没有更新minimumZoomScale和maximumZoomScale

然后我们需要手动执行willRotateToInterfaceOrientation:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGFloat ratioAspect = webview.bounds.size.width/webview.bounds.size.height;
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
        case UIInterfaceOrientationPortrait:
            // Going to Portrait mode
            for (UIScrollView *scroll in [webview subviews]) { //we get the scrollview 
                // Make sure it really is a scroll view and reset the zoom scale.
                if ([scroll respondsToSelector:@selector(setZoomScale:)]){
                    scroll.minimumZoomScale = scroll.minimumZoomScale/ratioAspect;
                    scroll.maximumZoomScale = scroll.maximumZoomScale/ratioAspect;
                    [scroll setZoomScale:(scroll.zoomScale/ratioAspect) animated:YES];
                }
            }
            break;
        default:
            // Going to Landscape mode
            for (UIScrollView *scroll in [webview subviews]) { //we get the scrollview 
                // Make sure it really is a scroll view and reset the zoom scale.
                if ([scroll respondsToSelector:@selector(setZoomScale:)]){
                    scroll.minimumZoomScale = scroll.minimumZoomScale *ratioAspect;
                    scroll.maximumZoomScale = scroll.maximumZoomScale *ratioAspect;
                    [scroll setZoomScale:(scroll.zoomScale*ratioAspect) animated:YES];
                }
            }
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!