Это сработало для меня. Это может помочь. Этот код будет соответствовать веб-представлению в любом представлении с соответствующими ограничениями.
import WebKit
var viewOfWeb : WKWebView?
let webViewConfiguration = WKWebViewConfiguration()
viewOfWeb = WKWebView(frame: .zero, configuration: webViewConfiguration)
viewOfWeb?.uiDelegate = self
viewOfWeb?.navigationDelegate = self
self.viewOfWebView?.addSubview(viewOfWeb ?? WKWebView())
AppModelUtils.sharedInstance.setConstraint(subView: viewOfWeb ?? WKWebView(), RelateviveToSecondView: viewOfWebView)
if let url = URL(string: urlString){
let request = URLRequest(url: url)
DispatchQueue.main.async{
self.stopSpinner()
self.webView.load(request)
}
}
}
func setConstraint(subView: UIView ,RelateviveToSecondView superView:UIView)
{
subView.translatesAutoresizingMaskIntoConstraints = false
let constX:NSLayoutConstraint = NSLayoutConstraint(item: subView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: superView, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0);
superView.addConstraint(constX);
let constY:NSLayoutConstraint = NSLayoutConstraint(item: subView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem:superView, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0);
superView.addConstraint(constY);
let constW:NSLayoutConstraint = NSLayoutConstraint(item: subView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: superView, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1, constant: 0);
superView.addConstraint(constW);
let constH:NSLayoutConstraint = NSLayoutConstraint(item: subView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: superView, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 0);
superView.addConstraint(constH);
}