缩放操作+ UIInterfaceOrientation更改后,iPhone UIWebView宽度不适合 [英] iPhone UIWebView width does not fit after zooming operation + UIInterfaceOrientation change

查看:144
本文介绍了缩放操作+ UIInterfaceOrientation更改后,iPhone UIWebView宽度不适合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a bare bones iPhone app with a UIWebView (Scales Page to Fit = YES, shouldAutorotateToInterfaceOrientation = YES) and loaded a webpage, e.g. https://stackoverflow.com/

旋转设备显示UIWebView是自动的 - 适合宽度。好。

Rotating the device shows that UIWebView is auto-resized to fit the width. Good.

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

Incorrect: Zoom into the page and zoom out. Now rotating the device shows UIWebView in a weird width in one of the orientation (if u zoom in landscape, the portrait width is weird, vice versa). This behavior is fixed only when you navigate to another page.

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

Correct: Load the same URL in Mobile Safari. Rotating works & the width fits regardless of the zooming exercise.

这是一个UIWebView错误(可能不是)吗?或者有什么东西需要做才能让事情像移动Safari一样正常工作?

Is this a UIWebView bug (probably not)? Or is there something that needs to be done to make things "just work" like in Mobile Safari?

推荐答案

我发现了一些东西为我工作。问题是,当uiwebview更改其方向时,Web内容会缩放以适合视口。但是scrollview子视图的zoomscale参数没有正确更新(也没有更新minimumZoomScale也没有更新maximumZoomScale

I found something that worked for me. The problem is that when uiwebview changes its orientation web contents are zoommed to fit with viewport. But zoomscale parameter of scrollview subview is not updated correctly (nor are updated minimumZoomScale nor 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;
    }
}

希望这会有所帮助!

这篇关于缩放操作+ UIInterfaceOrientation更改后,iPhone UIWebView宽度不适合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆