旋转后 UIWebView 内容未调整为新框架

UIWebView content not adjusted to new frame after rotation(旋转后 UIWebView 内容未调整为新框架)

本文介绍了旋转后 UIWebView 内容未调整为新框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件应用程序,
它显示 UISplitViewController 内的 UIWebView 中的内容.
一切正常,直到我旋转设备,当已经在 UIWebView 中放大/缩小.旋转设备时,我在

中调整 UIWebView 的框架

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration

问题是,当我在 UIWebView 中放大/缩小然后旋转设备时.
内容未根据新框架调整大小,这会导致内容中出现灰色/黑色边框,或者内容可以水平滚动.

附加信息:UIWebViewUIScrollView 上的子视图,可以滚动.当UIWebView完全可见时,UIScrollViewscrollEnabled被禁用,UIWebView可以滚动.p>

上图是选择邮件时的横向UIWebView.
这是两种情况下轮换前的状态.



[旋转前未缩放]上图显示 UIWebView 正确地将内容调整为新框架.



[旋转前缩放]上图为横向放大后旋转为纵向后的UIWebView-> 内容显示不正确


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation:

UIWebView.scrollView的信息<_UIWebViewScrollView: 0xb1322a0;帧 = (0 0; 768 960);clipsToBounds = YES;自动调整大小 = H;手势识别器 = <NSArray: 0xb1321d0>;层=<CALayer:0xb132270>;contentOffset: {0, 0}>框架是正确的!

解决方案!!
我设法通过在旋转设备后更新视口宽度来解决它

CGFloat fll_width = self.view.frame.size.width;NSString* adsl_javascript_string = [NSString stringWithFormat:@"var setViewPortScript = document.createElement('meta');setViewPortScript.setAttribute('name', 'viewport');setViewPortScript.setAttribute('content', 'width = %f');document.getElementsByTagName('head')[0].appendChild(setViewPortScript);", fll_width];[adsc_webview stringByEvaluatingJavaScriptFromString:adsl_javascript_string];

解决方案

我设法通过在旋转设备后更新视口宽度来解决它

CGFloat fll_width = self.view.frame.size.width;NSString* adsl_javascript_string = [NSString stringWithFormat:@"var setViewPortScript = document.createElement('meta');setViewPortScript.setAttribute('name', 'viewport');setViewPortScript.setAttribute('content', 'width = %f');document.getElementsByTagName('head')[0].appendChild(setViewPortScript);", fll_width];[adsc_webview stringByEvaluatingJavaScriptFromString:adsl_javascript_string];

I have an e-mail application,
which shows the content in an UIWebView inside an UISplitViewController.
Everything works fine until i rotate the device, when already zoomed in/out in the UIWebView. When rotating the device, i adjust the frame of the UIWebView in

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

The problem is, when i zoomed in/out in the UIWebViewand then rotate the device.
The content isn't resized to it's new frame, which leads to a gray/black border in the content, or the content can be scrolled horizontal.

Addional information: The UIWebViewis a subview on a UIScrollView which can be scrolled. When the UIWebViewis totally visible the scrollEnabledof the UIScrollViewis disabled and the UIWebView can be scrolled.

Above picture shows the UIWebViewin landscape when selecting an email.
This is the state before rotating in both cases.



[Not zoomed before rotating]Above picture shows that the UIWebView is correctly resizing the content to it's new frame.



[Zoomed before rotating]Above picture shows the UIWebView after zooming in landscape and then rotating to portrait -> content isn't correctly shown

Information of the UIWebView.scrollView in
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation:

<_UIWebViewScrollView: 0xb1322a0; frame = (0 0; 768 960); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0xb1321d0>; layer = <CALayer: 0xb132270>; contentOffset: {0, 0}> frame is correct!

Edit: Solution!!
i managed to solve it by updating the viewport width after rotating the device

CGFloat fll_width = self.view.frame.size.width; NSString* adsl_javascript_string = [NSString stringWithFormat:@"var setViewPortScript = document.createElement('meta'); setViewPortScript.setAttribute('name', 'viewport'); setViewPortScript.setAttribute('content', 'width = %f'); document.getElementsByTagName('head')[0].appendChild(setViewPortScript);", fll_width]; [adsc_webview stringByEvaluatingJavaScriptFromString:adsl_javascript_string];

解决方案

i managed to solve it by updating the viewport width after rotating the device

CGFloat fll_width = self.view.frame.size.width;
NSString* adsl_javascript_string = [NSString stringWithFormat:@"var setViewPortScript = document.createElement('meta');
     setViewPortScript.setAttribute('name', 'viewport');
     setViewPortScript.setAttribute('content', 'width = %f');
     document.getElementsByTagName('head')[0].appendChild(setViewPortScript);", fll_width];
[adsc_webview stringByEvaluatingJavaScriptFromString:adsl_javascript_string];

这篇关于旋转后 UIWebView 内容未调整为新框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:旋转后 UIWebView 内容未调整为新框架

基础教程推荐