2014-02-17 39 views
2

我有一个电子邮件应用程序,
它显示内的UISplitViewController内容。
一切正常,直到我旋转设备,当已经放大/在UIWebView。 当旋转装置中,我调整UIWebView的帧中
UIWebView内容在旋转后未调整为新帧

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

的问题是,当我在UIWebView放大/缩小,然后旋转该装置。
内容未被调整为新框架的大小,这会导致内容中出现灰色/黑色边框,或者内容可能会水平滚动。

Addional information: UIWebView是可以滚动的UIScrollView的子视图。 当UIWebView完全可见时,UIScrollViewscrollEnabled被禁用,并且可以滚动UIWebView

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


Portrait_Without_Zooming
上面的照片示出了UIWebView被正确调整所述内容到它的新的帧[不旋转之前放大。


Portrait_With_Zooming
横向缩放和旋转,然后以肖像 后上面图为UIWebView [旋转之前已缩放] - 在
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation>内容不正确显示

UIWebView.scrollView的信息:

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

+0

你知道,一个'UIWebView'是UIScrollView'的'所以我只是想知道的逻辑是什么,具有额外'子类UIScrollView'您所描述的所有内容都可以通过“UIWebView”来实现, – Popeye

回答

3

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

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

这似乎是一个错误也在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]; 
    }); 
}