【问题标题】:Show UIView programmatically when no posts gotten没有帖子时以编程方式显示 UIView
【发布时间】:2016-12-02 11:56:00
【问题描述】:

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.'

【问题讨论】:

  • 崩溃报告是什么
  • edit您的问题,而不是评论如此复杂的内容。谢谢。
  • @EricAya 对不起。已编辑的问题。
  • @D.Finna 没问题。感谢您的编辑。 :)

标签: ios swift uitableview uiview


【解决方案1】:

tableView.addSubview(noPostView) 置于约束之上。

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

tableView.addSubview(noPostView)

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

【讨论】:

  • 做到了!你知道我怎样才能使 UIView 居中吗? :-)
  • 使用centerXAnchor centerYAnchor 而不是useyourloaf.com/blog/pain-free-constraints-with-layout-anchors
  • 我这样做了:noPostView.anchorToTop(tableView.centerYAnchor, left: tableView.centerXAnchor, bottom: nil, right: nil) 但它位于左上角的中心,而不是 UIView 的中间 :)
  • 好吧!在该代码中,您将noPostView.topAnchortableView.centerYAnchor 对齐:) 您是否检查了使用您的面包链接?
  • noPostView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor).isActive = true 我觉得你可以选择那个,伙计 :)
猜你喜欢
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多