2016-12-02 46 views
0

我想显示一个UIView,其中有一个标签,说:“你没有帖子”,当update.count == 0。我试图做到这一点,但当我尝试时,应用程序崩溃。以编程方式显示UIView,当没有帖子得到

我做错了什么?这里是我的代码:

let noPostView: UIView = { 
     let iv = UIView() 
     iv.backgroundColor = UIColor.whiteColor() 

     //iv.image = UIImage(named: "iphoneSample") 
     //iv.clipsToBounds = true 
     return iv 
    }() 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if updates.count == 0 { 
      noPostView.anchorToTop(tableView.topAnchor, left: tableView.leftAnchor, bottom: tableView.bottomAnchor, right: tableView.rightAnchor) 
      noPostView.heightAnchor.constraintEqualToConstant(100).active = true 
      noPostView.widthAnchor.constraintEqualToConstant(100).active = true 


      tableView.addSubview(noPostView) 
     } 
     return updates.count 
    } 

得到这个错误当中更多:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x61000067f280 "UIView:0x7fdbcbd21490.top"> and <NSLayoutYAxisAnchor:0x610000660a40 "UITableView:0x7fdbcd86a600.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.' 
+0

什么崩溃报告 –

+0

请编辑您的问题,而不是评论这样复杂的内容。谢谢。 – Moritz

+0

@EricAya对不起。编辑的问题。 –

回答

1

tableView.addSubview(noPostView)您的约束上面。

如果你对所有4个方面都有限制,你应该不需要heightAnchorwidthAnchor吗?

tableView.addSubview(noPostView) 

noPostView.anchorToTop(tableView.topAnchor, left: tableView.leftAnchor, bottom: tableView.bottomAnchor, right: tableView.rightAnchor) 
+0

这样做!你知道我能如何集中UIView吗? :-) –

+0

使用'centerXAnchor''centerYAnchor'来代替http://useyourloaf.com/blog/pain-free-constraints-with-layout-anchors/ – Magoo

+0

我这样做了:'noPostView.anchorToTop(tableView.centerYAnchor,left: tableView.centerXAnchor,bottom:nil,right:nil)'但是它居中左上角,而不是UIView的中间:) –

相关问题