旋转后UIWebView内容未调整为新帧
我有一个电子邮件应用程序,
它显示了UISplitViewController
内的UIWebView
中的内容。
一切正常,直到我旋转设备,当在UIWebView
放大/缩小时。 旋转设备时,我调整了UIWebView
的框架
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
问题是,当我在UIWebView
放大/缩小然后旋转设备时。
内容未调整为新帧的大小,导致内容中出现灰色/黑色边框,或者内容可以水平滚动。
附加信息: UIWebView
是UIScrollView
上的子视图,可以滚动。 当UIWebView
完全可见时, UIScrollView
的scrollEnabled
被禁用, UIWebView
可以滚动。
上图显示了选择电子邮件时的横向UIWebView
。
这是两种情况下旋转前的状态。
[旋转前未缩放]上图显示UIWebView
正在将内容正确调整为新帧的大小。
[旋转前放大]上图显示了放大横向然后旋转到纵向后的UIWebView
– >内容未正确显示
UIWebView.scrollView
信息
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
:
<_UIWebViewScrollView: 0xb1322a0; frame = (0 0; 768 960); clipsToBounds = YES; autoresize = H; gestureRecognizers = ; layer = ; contentOffset: {0, 0}>
<_UIWebViewScrollView: 0xb1322a0; frame = (0 0; 768 960); clipsToBounds = YES; autoresize = H; gestureRecognizers = ; layer = ; 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];
这似乎也是iOS 7和iOS 8中的一个错误。修复将是这样的:
首先在布局更改时调整框架大小:
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _webView.frame = self.view.bounds; }
然后在旋转回调上将zoomScale重置为0.是,0!
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // Allow the animation to complete dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [_webView.scrollView setZoomScale:0 animated:YES]; }); }
您也可以使用新function
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { // Allow the animation to complete dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [_webView.scrollView setZoomScale:0 animated:YES]; }); }