【问题标题】:UIWebView content not adjusted to new frame after rotation旋转后 UIWebView 内容未调整为新框架
【发布时间】:2014-03-16 10:16:53
【问题描述】:

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

的框架

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

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

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

上图显示了选择电子邮件时的UIWebViewin 横向。
这是两种情况下旋转前的状态。



[旋转前未缩放]上图显示UIWebView正在正确调整大小内容到它的新框架。



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

UIWebView.scrollView的信息
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation:

<_UIWebViewScrollView: 0xb1322a0; frame = (0 0; 768 960); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0xb1321d0>; layer = <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];

【问题讨论】:

  • 您知道UIWebViewUIScrollView 的子类,所以我只是想知道额外的UIScrollView 的逻辑是什么,您所描述的一切都可以通过@987654346 来实现@,

标签: ios objective-c ipad uiwebview uiscrollview


【解决方案1】:

这似乎也是 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];
    });
}

你也可以使用新功能

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)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];
    });
}

【讨论】:

    【解决方案2】:

    我通过在旋转设备后更新视口宽度来解决这个问题

    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 2011-08-31
      • 1970-01-01
      • 2011-08-11
      • 2013-03-26
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多