iPhone UIWebView width does not fit after zooming operation + UIInterfaceOrientation change(缩放操作 + 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 宽度不适合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:缩放操作 + UIInterfaceOrientation 更改后 iPhone UIWebView 宽度不适合
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01