为什么我不能将在 viewDidLoad 方法中创建的视图约束到 ViewControllers 视图?
Why can I not constrain a view created in the viewDidLoad method to the ViewControllers View?
以下是我在 ViewController 中初始化一个 UIView 然后将其约束到 ViewController 的主视图后收到的错误消息。
Unable to activate constraint with anchors because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.
如果您查看我提供的图片,您会发现在情节提要中我可以在主视图中放置另一个 UIView,然后我可以将它限制在主视图的边缘。这工作得很好,但我想知道如何在代码中做同样的事情。仅使用 NSLayoutConstraint 并将其固定到顶部、底部、前导和尾部似乎对我不起作用。任何对此的见解都会很棒。
let webView: WKWebView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: webView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .bottom,
relatedBy: .equal,
toItem: view,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0).isActive = true
let url = NSURL (string: someURL);
let requestObj = NSURLRequest(url: url! as URL);
webView.load(requestObj as URLRequest);
}
您似乎忘记在添加约束之前将 webView
添加到 view
:
view.addSubview(webView)
最终代码:
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
NSLayoutConstraint(item: webView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .bottom,
relatedBy: .equal,
toItem: view,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0).isActive = true
let url = NSURL (string: "");
let requestObj = NSURLRequest(url: url! as URL);
webView.load(requestObj as URLRequest);
以下是我在 ViewController 中初始化一个 UIView 然后将其约束到 ViewController 的主视图后收到的错误消息。
Unable to activate constraint with anchors because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.
如果您查看我提供的图片,您会发现在情节提要中我可以在主视图中放置另一个 UIView,然后我可以将它限制在主视图的边缘。这工作得很好,但我想知道如何在代码中做同样的事情。仅使用 NSLayoutConstraint 并将其固定到顶部、底部、前导和尾部似乎对我不起作用。任何对此的见解都会很棒。
let webView: WKWebView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: webView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .bottom,
relatedBy: .equal,
toItem: view,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0).isActive = true
let url = NSURL (string: someURL);
let requestObj = NSURLRequest(url: url! as URL);
webView.load(requestObj as URLRequest);
}
您似乎忘记在添加约束之前将 webView
添加到 view
:
view.addSubview(webView)
最终代码:
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
NSLayoutConstraint(item: webView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: webView,
attribute: .bottom,
relatedBy: .equal,
toItem: view,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0).isActive = true
let url = NSURL (string: "");
let requestObj = NSURLRequest(url: url! as URL);
webView.load(requestObj as URLRequest);