【问题标题】:WKWebView show content wrong frame in old projectWKWebView 在旧项目中显示内容错误的框架
【发布时间】:2021-06-10 10:17:27
【问题描述】:

我有一个使用 Objective C 的旧 iOS 项目。当我将 WKWebView 添加到 ViewController 时

    NSURL *url = [NSURL URLWithString:@"https://www.google.com"];

    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];

    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:theConfiguration];
    [self.view addSubview:_webView];
    
    self.webView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        
                                             [self.webView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
                                             [self.webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
                                             [self.webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
                                             [self.webView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
    ]];

    [_webView loadRequest:[NSURLRequest requestWithURL:url]];

如果我在 iOS 14 中通过 Xcode 安装应用程序,webview 工作正常,但在我断开 Xcode 并直接打开应用程序后,webview 显示内容的框架错误。如果我通过 Xcode 在 iOS 12.5 中构建应用程序,Web 视图也会显示错误框架的内容。 如果我从 Xcode 12.5 创建新项目并使用相同的代码,它工作正常。

这是通过 Xcode 构建打开应用时的屏幕截图

https://i.stack.imgur.com/rYb3M.png

这是直接打开应用时的截图:

https://i.stack.imgur.com/4q0uD.png

【问题讨论】:

    标签: ios objective-c xcode wkwebview


    【解决方案1】:

    一些观察 -

    1. 此代码混合使用了_webViewself.webView。为清楚起见,您应该至少统一这个代码块的用法。

    2. 你到底在哪里称呼这部分? viewDidLoad() 或在某些事件之后?添加后可以尝试添加layoutIfNeeded() 调用吗?

    /// After activating constraints
    [self.view layoutIfNeeded];
    
    1. 您应该尝试分别使用leadingAnchortrailingAnchor 而不是leftAnchorrightAnchor

    如果没有任何帮助,我认为您的视图控制器的视图情节提要设置未更新为使用自动布局。在这种情况下,你可以试试这个 -

    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:theConfiguration];
    _webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [self.view addSubview:_webView];
    

    请注意,在这种情况下 - 您必须删除所有与约束相关的代码。

    /*
    self.webView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[ ...... ]];
    */
    

    【讨论】:

    • -'ViewController' 没有可见的@interface 声明选择器'requiresConstraintBasedLayout' 但是如果我不使用自动布局,而只使用框架,它仍然是同样的问题。
    • 为了测试,我在viewDidApear中写了这段代码,layoutIfNeeded也没用
    • 哦,对不起,当我使用 self.view 时,它显示同样的错误。 “UIView”没有可见的@interface 声明选择器“requiresConstraintBasedLayout”
    • 在“UIView *”类型的对象上找不到属性“requiresConstraintBasedLayout”
    • webview 框架仍然是真的,即使里面的滚动视图和 WKWebViewContent 框架仍然是真的,但是下一级子视图是错误的框架
    【解决方案2】:

    经过几天的搜索,我终于找到了错误的原因。 在 UIView 扩展文件中重写 center 属性

    #import <UIKit/UIKit.h>
    
    @interface UIView (FrameExtension)
    
    @property CGPoint center;
    
    @end
    

    .

    #import "UIView+FrameExtension.h"
    
    @implementation UIView (FrameExtension)
    
    - (CGPoint)center {
         return CGPointMake(self.frame.origin.x + self.frame.size.width / 2,
                            self.frame.origin.y + self.frame.size.height / 2);
    }
    
    - (void)setCenter:(CGPoint)center {
         CGRect frame = self.frame;
         frame.origin = CGPointMake(center.x - self.frame.size.width / 2,
                                    center.y - self.frame.size.height / 2);
         self.frame = frame;
    }
    
    @end
    

    不知何故,在加载 webview 时,它调用了导致错误的 setCenter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      相关资源
      最近更新 更多