为什么 WKWebView 没有出现在屏幕上? ( objective c )

Why WKWebView doesn't apear in screen ? ( objective c )

我正在尝试在 ViewController 中添加 UIView 和 WKWebView,我以编程方式添加了约束。

UIView 显示和定位没有任何问题,但 WKWebView 只出现 0.5 秒然后消失。如果 WKWebView 为空,即使我设置了宽度和高度,也可能不会出现在屏幕上吗?

你能帮我找出错误吗?

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    NSMutableArray *arrayContraints = [[NSMutableArray alloc] init];

    [super viewDidLoad];

    self.dialogView = [[UIView alloc] initWithFrame:CGRectZero];
    self.panelWebView = [[WKWebView alloc] initWithFrame:CGRectZero];

    _dialogView.backgroundColor = UIColor.redColor;
    _panelWebView.backgroundColor = UIColor.greenColor;

    [self.view addSubview:_panelWebView];
    [self.view addSubview:_dialogView];

    [self.dialogView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.panelWebView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.dialogView
                                                                        attribute: NSLayoutAttributeHeight
                                                                        relatedBy: NSLayoutRelationEqual
                                                                           toItem: nil
                                                                        attribute: NSLayoutAttributeNotAnAttribute
                                                                       multiplier: 1.0
                                                                         constant: 200]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.dialogView
                                                                        attribute: NSLayoutAttributeWidth
                                                                        relatedBy: NSLayoutRelationEqual
                                                                           toItem: nil
                                                                        attribute: NSLayoutAttributeNotAnAttribute
                                                                       multiplier: 1.0
                                                                         constant: 100]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.dialogView
                                                                        attribute: NSLayoutAttributeLeading
                                                                        relatedBy: NSLayoutRelationEqual
                                                                           toItem: self.dialogView.superview
                                                                        attribute: NSLayoutAttributeLeading
                                                                        multiplier: 1.0
                                                                         constant: 80]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.dialogView
                                                                        attribute: NSLayoutAttributeTop
                                                                        relatedBy: NSLayoutRelationEqual
                                                                           toItem: self.dialogView.superview
                                                                        attribute: NSLayoutAttributeTop
                                                                       multiplier: 1.0
                                                                         constant: 80]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.panelWebView
                                                             attribute: NSLayoutAttributeWidth
                                                             relatedBy: NSLayoutRelationEqual
                                                                toItem: nil
                                                             attribute: NSLayoutAttributeNotAnAttribute
                                                            multiplier: 1.0
                                                              constant: 100]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.panelWebView
                                                             attribute: NSLayoutAttributeHeight
                                                             relatedBy: NSLayoutRelationEqual
                                                                toItem: nil
                                                             attribute: NSLayoutAttributeNotAnAttribute
                                                            multiplier: 1.0
                                                              constant: 100]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.panelWebView
                                                             attribute: NSLayoutAttributeTop
                                                             relatedBy: NSLayoutRelationEqual
                                                                toItem: self.panelWebView.superview
                                                             attribute: NSLayoutAttributeTop
                                                            multiplier: 1.0
                                                              constant: 80]];

    [arrayContraints addObject: [NSLayoutConstraint constraintWithItem: self.panelWebView
                                                             attribute: NSLayoutAttributeLeading
                                                             relatedBy: NSLayoutRelationEqual
                                                                toItem: self.panelWebView.superview
                                                             attribute: NSLayoutAttributeLeading
                                                            multiplier: 1.0
                                                              constant: 260]];


    [NSLayoutConstraint activateConstraints: arrayContraints];

    [self.dialogView layoutIfNeeded];
    [self.panelWebView layoutIfNeeded];
    [self.dialogView.superview layoutIfNeeded];

}
@end

如果 WKWebView 为空,那么即使您设置了所有约束,它也不会显示在屏幕上。