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

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

问题描述

我用 UIWebView 创建了一个简单的 iPhone 应用程序(缩放页面以适应 = 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 中加载相同的 URL.旋转作品&无论缩放练习如何,宽度都适合.

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

这是一个 UIWebView 错误吗(可能不是)?或者是否需要做一些事情才能让事情像在 Mobile 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天全站免登陆