以编程方式添加容器视图而不使用故事板?

时间:2016-08-13 19:58:23

标签: ios swift xcode7 uicontainerview

我在viewDidLoad

中有这个
    let childViewControllerForPosts = PostsCollectionViewController(collectionViewLayout: UICollectionViewLayout())
    let cView = childViewControllerForPosts.view
    self.view.backgroundColor = UIColor.redColor()
    self.addChildViewController(childViewControllerForPosts)
    self.view.addSubview(cView)
    childViewControllerForPosts.didMoveToParentViewController(self)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleScroll(_:)), name: "cvScrolled", object: nil)

    cView.widthAnchor.constraintEqualToConstant(view.frame.size.width).active = true
    cView.heightAnchor.constraintEqualToConstant(view.frame.size.height).active = true
    cView.topAnchor.constraintEqualToAnchor(containerForGreyAndPurple.bottomAnchor).active = true
    cView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true

然而,我得到一个错误,说这是非法约束,因为它没有共同的祖先:(

这是错误:

...because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal.'

0 个答案:

没有答案