旋转UIView并填充屏幕

时间:2015-08-26 15:44:57

标签: ios objective-c layout webview wkwebview

在我的iOS应用程序中,我必须有条件地旋转WKWebView并希望WebView填满整个屏幕(减去顶部工具栏)。我可以旋转WebView,但它似乎没有填满整个屏幕。

这就是我目前所拥有的:

启动WebView:

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中,我收到服务器的响应,确定是否需要旋转WebView:

- (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

1 个答案:

答案 0 :(得分:0)

我不确定它,因为我没有对它进行测试,但是在转换之前无需更改帧,因为您设置了自动布局,删除了帧线,如果它不起作用,您可以重置通过删除前面的那些并创建新的

,转换后的webview约束