iPhone UIWebView 宽度在缩放操作后不适合 + UIInterface方向改变
我使用 UIWebView 创建了一个简单的 iPhone 应用程序(缩放页面以适合 = YES,shouldAutorotateToInterfaceOrientation = YES)并加载了一个网页,例如 https://stackoverflow.com /
旋转设备显示 UIWebView 会自动调整大小以适应宽度。好的。
错误:放大页面然后缩小。现在旋转设备会在其中一个方向上显示 UIWebView 的宽度很奇怪(如果你放大横向,则纵向宽度很奇怪,反之亦然)。仅当您导航到另一个页面时,此行为才会修复。
正确:在 Mobile Safari 中加载相同的 URL。旋转作品和无论缩放练习如何,宽度都适合。
这是 UIWebView 错误吗(可能不是)?或者是否需要做一些事情才能让事情像在 Mobile Safari 中一样“正常工作”?
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/
Rotating the device shows that UIWebView is auto-resized to fit the width. Good.
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.
Correct: Load the same URL in Mobile Safari. Rotating works & the width fits regardless of the zooming exercise.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我找到了对我有用的东西。问题是,当 uiwebview 更改其方向时,Web 内容会缩放以适应视口。但是滚动视图子视图的zoomscale参数没有正确更新(也没有更新minimumZoomScale和maximumZoomScale
那么我们需要在
willRotateToInterfaceOrientation
处手动执行:希望这有帮助!
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
Then we need to do it manually at
willRotateToInterfaceOrientation
:Hope this helps!
我已经尝试过 M Penades 的解决方案,这似乎也对我有用。
我遇到的唯一问题是,当在 3G 上运行时,轮换不是很顺利。
因此,我现在使用不同的方法:
最好的问候,
拉尔夫
I've tried the solution from M Penades and this seems to work for me as well.
The only issue that I'm experiencing is that when running this on a 3Gs the rotation is unfortunately not very smooth.
I'm therefore now using a different approach:
Best Regards,
Ralph
尝试此代码,它会微不足道地更改缩放级别(1.01)以允许 UIWebView 在横向模式下增加内容大小
findScrollViewInsideView:添加方法以支持 ios4
try this code, it insignificantly changes zoom level (1.01) to allow UIWebView increase content size in landscape mode
findScrollViewInsideView: method added to support ios4
我有一个解决这个问题的方法,但我得说我不是它的忠实粉丝。它效果很好,但该解决方案实际上会导致另一个问题。我已经解决了第二个问题,但这需要一些努力。
请记住,自从 OS3.2 或 iOS4(不确定是哪一个)以来,UIWebView 的直接子视图现在是 UIScrollView 而不是 UIScroller,因此我们可以用它做更多的事情。另外,由于访问 View 的子视图不是私有操作,因此使用转换为文档化视图的子视图也不是私有操作,我们可以在不违反规则的情况下使用 UIWebView 做很多事情。
首先,我们需要从 UIWebview 获取 UIScrollView:
现在我们需要更改此滚动视图的委托,以便我们可以覆盖滚动视图委托调用(这实际上可能是此解决方案导致的次要错误的原因,我会稍后分享):
现在,如果您此时尝试,缩放功能就会被破坏。我们需要实现一个 UIScrollViewDelegate 方法来修复它。添加:
webBrowserView 实际上是一个 UIWebBrowserView,但这不是一个记录的类,所以我们只是将其视为 UIView。
现在运行您的应用程序,放大然后缩小网页。旋转,它应该正确显示。
这确实会导致一个相当大的错误,可能比原来的更糟糕。
如果放大然后旋转,您将失去滚动能力,但您的视图仍会放大。这是完成整个事情的修复。
首先,我们需要跟踪一些数字,并定义一个标志:
我在我的 h 文件中定义了这些数字:
您可能认为您可以从 UIScrollView 中获取这些数字,但是当您需要它们时,它们会发生变化,所以我们需要将它们存储在其他地方。
我们需要使用另一种委托方法:
现在我觉得它变得一团糟。
我们需要跟踪旋转,因此您需要将其添加到 viewDidLoad、loadView 或用于注册通知的任何方法中:
并创建此方法:
所以现在只要您旋转 webViewOrientationChange 就会被调用。 PerformSelector 延迟 0.0 秒的原因是我们想在下一个运行循环中调用 adjustmentWithZoomData。如果直接调用它,则 adjustmentWithZoomData 将调整为之前的方向。
这是 adjustWithZoomData 方法:
就是这样!现在,当您旋转时,它将保持缩放,并大致保持正确的偏移。如果有人想计算如何获得精确的正确偏移量,那就去做吧!
I have a solution to this problem, but I gotta say I'm not a huge fan of it. It works great, but the solution actually causes another problem. I have a fix for the secondary issue, but it takes a bit of effort.
Just keep in mind that since OS3.2 or iOS4 (not sure which) UIWebView's direct subview is now UIScrollView instead of UIScroller, so we can do a lot more with it. Also, since accessing subviews of a View is not a private action, neither is using a subview that is casted as a documented view we can do a lot with the UIWebView without breaking the rules.
First we need to get the UIScrollView from the UIWebview:
Now we need to change the delegate of this scrollview so we can override scrollview delegate calls (which may actually be the cause of a secondary bug as a result of this solution, which I'll share in a moment):
Now, if you try it at this point, zooming is broken. We need to implement a UIScrollViewDelegate method to fix it. add:
webBrowserView is actually a UIWebBrowserView, but that isn't a documented class, so we are just going to treat it as a UIView.
Now run your app, zoom in and then zoom out the webpage. Rotate, and it should appear correctly.
This does cause a rather large bug, that is perhaps worse than the original.
If you zoom in and then rotate, you will loose scrolling ability, but your view will be zoomed in still. Here is the fix To complete the whole thing.
First, we need to keep track of a few numbers, and have a flag defined:
I have these defined in my h file:
You may think you can just grab these numbers from UIScrollView, but when you need them, they will have changed, so we need them stored elsewhere.
We need to use another delegate method:
Now it gets into a mess I feel.
We need to track rotation, so you'll need to add this to your viewDidLoad, loadView, or whatever method you use to register notifications:
and create this method:
So now anytime you rotate webViewOrientationChange will be called. The reason performSelector is delayed for 0.0 seconds is because we want to call adjustWithZoomData on the next runloop. If you call it directly, the adjustWithZoomData will adjust for the previous orientation.
Here is the adjustWithZoomData method:
Thats it! Now when you rotate it will maintain zoom, and roughly maintain the correct offset. If anyone wants to do the math on how to get the exact correct offset then go for it!
我自己正在研究这个问题,并发现了一些更多信息:
更改缩放时的问题:
例如,如果当前宽度为 1024,并且您想在横向中从 1 缩放到 1.5,您可以:
I was looking into this myself and found out some more information:
Issues when changing zoom:
e.g. if current width is 1024 and you want to zoom from 1 to 1.5 in landscape you could:
所以显然我最终没有使用 M Penades 的解决方案(并且忘记更新这篇文章!抱歉)。
我所做的是调整整个文档的大小(并更改字体大小以保持比例)。这显然解决了问题。
但是,我的 UIWebView 仅用于加载我自己的 HTML 和 HTML。来自 iOS 文件系统的 CSS - 如果您正在构建通用 Web 浏览器,则此技巧可能不起作用。
ViewController.m
和 app.css
要点位于 https://gist.github.com/d6589584944685909ae5
So apparently I didn't use the solution by M Penades in the end (and forgot to update this post! sorry).
What I did was to resize the entire document (and change my font-size to keep things proportionate). That apparently fixed the issue.
However, my UIWebView is only for loading my own HTML & CSS from the iOS filesystem - if you're building a general purpose web browser, this trick may not work as well.
ViewController.m
And app.css
Gist at https://gist.github.com/d6589584944685909ae5
我发布此内容是因为我也遇到了同样的问题,在这里我遵循
M Penades
方法。M Penades
的答案仅适用于用户不这样做的情况倾斜(捏出)Webview,然后旋转设备并重复此过程。然后 UiwebView 的内容大小逐渐减小。这就是M Penades
答案中出现的问题。所以我也解决了这个问题,我的代码如下。1)为此,我设置了捏合手势,以便当用户倾斜时 UIwebView 可以检查 UIwebView 的缩放尺寸。
//One This Please import The
UIGestureRecognizerDelegate
Protocol in '.h file'返回 YES 保证允许同时识别。返回 NO 不能保证阻止同时识别,因为其他手势的代表可能会返回 YES
如果用户在这种情况下捏合/拉出 Web 视图,只需设置缩放因子。
这样 WebView 就可以根据内容的变化调整其 ContentSize。
即使用户倾斜 UIWebView,这也非常有效。
I am posting this because i have also faced the same problem and here i am following the
M Penades
Approach.M Penades
's Answer woks good only for case if user does not Skew(pinch Out) the Webview then rotate the device and repeat this process .then Content Size of UiwebView gets reduce gradually. so that was the issue came inM Penades
Answer. so I have fixed that issue too and my code is as below.1) For This I set the Pinch Gesture so that when User Skew The UIwebView could check the Scaled size of UIwebView.
//One This Please import The
UIGestureRecognizerDelegate
Protocol in '.h file'Returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
If User Hase Pinch In/Out The Web View in that Case Just Set THat Zooming Factor .
SO that WebView Can Adjust Its ContentSize as Oreintaion Changed.
This Works perfectly for even user skew the UIWebView.
旋转时,尝试将scrollView的zoomScale设置为0。
请在此处查看我的完整答案: UIWebView 内容未调整旋转后到新框架
On rotation, try setting the scrollView zoomScale to 0.
See my full answer here: UIWebView content not adjusted to new frame after rotation