为什么我不能将viewDidLoad方法中创建的视图约束到ViewControllers视图?

时间:2017-04-05 00:21:06

标签: ios swift uiviewcontroller nslayoutconstraint

以下是我在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.

enter image description here

如果你看一下我提供的图片,你会看到在故事板中我可以在主视图中删除另一个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);
    }

1 个答案:

答案 0 :(得分:3)

您似乎忘记在添加约束之前将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);