0

an error occurred while trying to run webview on xib using PageViewController.

Thread 1: "NSLayoutConstraint for <WKWebView: 0x14d0a8e00; frame = (0 0; 0 0); layer = <CALayer: 0x28109b600>>: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs."

I made a view in xib (Not a ..cell. This is a view.) and made a wkwebview with code using that view, but I am having a hard time because of these errors. Is there a solution? Below is the code I wrote.

CGRect rect = [[UIScreen mainScreen]bounds];
//CGRect rect = CGRectMake(0.0, 0.0, self.safeAreaContainerView.layer.frame.size.width, self.safeAreaContainerView.layer.frame.size.height);  //I only received width and height as 0 and annotated it.
self.wkwebview = [[WKWebView alloc] initWithFrame:rect];
self.wkwebview.UIDelegate = self;
self.wkwebview.navigationDelegate = self;
self.wkwebview.translatesAutoresizingMaskIntoConstraints = NO;

[self.wkwebview sizeToFit];
self.wkwebview.scrollView.scrollEnabled= YES;
[self.safeAreaContainerView addSubview:self.wkwebview];
[self.safeAreaContainerView sendSubviewToBack:self.wkwebview];

//WKWebView Layout Setting
NSLayoutConstraint *firstViewConstraintTop = [NSLayoutConstraint constraintWithItem:self.wkwebview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.safeAreaContainerView attribute:NSLayoutAttributeTop multiplier:1 constant:0];

NSLayoutConstraint *firstViewConstraintBottom = [NSLayoutConstraint constraintWithItem:self.safeAreaContainerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.wkwebview attribute:NSLayoutAttributeBottom multiplier:1 constant:0];

NSLayoutConstraint *firstViewConstraintLeadingSpace = [NSLayoutConstraint constraintWithItem:self.wkwebview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.safeAreaContainerView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];

NSLayoutConstraint *firstViewConstraintTrailingSpace = [NSLayoutConstraint constraintWithItem:self.safeAreaContainerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.wkwebview attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];

[self.safeAreaContainerView addConstraints:[NSArray arrayWithObjects: firstViewConstraintTop, firstViewConstraintBottom, firstViewConstraintLeadingSpace, firstViewConstraintTrailingSpace, nil]];

Does it not work in View when coding the WKWebView to the XIB? Is it only available on TableViewCell or CollectionViewCell?

Hee
  • 11
  • 3
  • Try adding ```self.wkwebview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;``` as part of your configuration. If you set up the constraints correctly (I did not check in detail) you can create that with ```CGRectZero``` and it should work. – skaak Oct 22 '20 at 07:09
  • I tried this method, but it still won't run. In addition, autoresize=W+H was added to the error content. – Hee Oct 22 '20 at 07:22

1 Answers1

0

I do something similar and use the code I posted here to embed the webview into its container view.

How to: Constrain subview to safeArea Obj-c

Apart from using that I do preciously little on the webview itself, basically the following

webView = [[WKWebView alloc] initWithFrame:GCRectZero];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

and nothing else.

HIH

skaak
  • 2,988
  • 1
  • 8
  • 16
  • I'm Sorry. Do you mean creating a View in the containerView? tried to create ContainerView on the XIB, but it did not exist. + ( void ) embed:( UIView * ) child into:( UIView * ) parent child = View, parent= ContainalView ? Is the above correct? – Hee Oct 22 '20 at 08:09
  • The child = webview, the parent = web container view. In your case it looks like child = self.wkwebview and parent = self.safeAreaContainerView. – skaak Oct 22 '20 at 14:04
  • Thanks to you, I solved it. I don't know if the method I implemented is the right answer. After running the Xib through caculating the size of RootView with CGRect, there has been no error.! – Hee Oct 23 '20 at 00:24
  • @Hee Nice I am glad and thank you for letting me know - I appreciate it. – skaak Oct 23 '20 at 03:52