【问题标题】:Rotate UIView and fill screen旋转 UIView 并填充屏幕
【发布时间】:2015-08-26 15:44:56
【问题描述】:

在我的 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 没有完全拉伸(高度方向)以适应屏幕(它应该覆盖它后面的 tableview):

【问题讨论】:

  • 你有没有尝试将框架代码放在主线程上?
  • 在 UIWindow 的子视图上添加 webView 怎么样(而不是在 self.view 的子视图上添加)?
  • 你不能在自动布局开启时改变框架。看看它stackoverflow.com/a/25423850/3767017

标签: ios objective-c layout webview wkwebview


【解决方案1】:

我不确定,因为我没有测试过,但是由于您设置了自动布局,因此无需在转换之前更改框架,删除框架线,如果它不起作用,您可以重置转换后的 webview 约束,通过删除以前的约束并创建新的约束

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多