2015-08-26 73 views
0

在我的iOS應用程序中,我必須有條件地旋轉WKWebView並希望WebView填充整個屏幕(減去頂部工具欄)。我可以旋轉WebView,但它似乎並沒有填滿整個屏幕。旋轉UIView並填充屏幕

這是我目前有:

發起web視圖:

self.webView = [[WKWebView alloc]initWithFrame:CGRectZero]; 
self.webView.navigationDelegate = self; 

[self.view addSubview:self.webView]; 

self.webView.translatesAutoresizingMaskIntoConstraints = NO; 

height = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; 

width = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; 

[self.view addConstraint:height]; 
[self.view addConstraint:width]; 
[self.webView loadRequest:request]; 

然後在didFinishNavigation,我接收來自其確定web視圖是否需要被旋轉時,服務器的響應:

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation 
{ 
    NSURLComponents *params = [NSURLComponents componentsWithURL:self.webView.URL resolvingAgainstBaseURL:NO]; 
    NSArray *queryItems = params.queryItems; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", @"tree_width"]; 
    NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] 
           firstObject]; 
    int tree_width = [queryItem.value intValue]; 

    if(tree_width > 10){ 
     // rotate WebView 

     self.webView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); 
     self.webView.transform = CGAffineTransformMakeRotation(M_PI_2); 
    } 
} 

您可以看到WebView未完全拉伸(高度方向)以適應屏幕(它應該掩蓋其背後的實現代碼如下):

enter image description here

+0

剛剛你試圖把框架代碼放在主線程? – Jasper

+0

如何在UIWindow的子視圖上添加webView(而不是將其添加到self.view的子視圖中)? –

+0

當自動佈局打開時,您無法更改框架。請參閱http://stackoverflow.com/a/25423850/3767017 –

回答

0

我不知道這件事,因爲我沒有測試它,但沒有必要改變幀之前的變換,因爲你設置自動佈局,刪除框架線,如果它不工作,你可以重置webview的約束後,通過刪除以前的和創建新的