2014-02-17 33 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]; 
    }); 
}