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未完全拉伸(高度方向)以適應屏幕(它應該掩蓋其背後的實現代碼如下):
剛剛你試圖把框架代碼放在主線程? – Jasper
如何在UIWindow的子視圖上添加webView(而不是將其添加到self.view的子視圖中)? –
當自動佈局打開時,您無法更改框架。請參閱http://stackoverflow.com/a/25423850/3767017 –