【问题标题】:Remove programmatically UILabel after adding - swift添加后以编程方式删除 UILabel - swift
【发布时间】:2017-04-17 12:12:49
【问题描述】:

我以编程方式制作了一个 UIView 和两个 UILabel。标签上的内容类似于:“没有帖子,请关注某人以添加到您的提要中。”。

self.update.count 显示要显示的帖子数。所以如果是0 应该显示我制作的标签。如果不是,则不应显示任何标签。

我已经编写了这段代码,但它不会再次删除标签和 UIView?它在viewWillAppear 里面:

if self.updates.count == 0 {

            print("THERE ARE NO POSTS: \(self.updates.count)")
            self.tableView.addSubview(self.noPostView)
            self.tableView.addSubview(self.noPostLabel)
            self.tableView.addSubview(self.noPostText)

            //noPostView.anchorToTop(view.topAnchor, left: nil, bottom: view.bottomAnchor, right: nil)
            self.noPostView.centerXAnchor.constraintEqualToAnchor(self.tableView.centerXAnchor).active = true
            //noPostView.centerYAnchor.constraintEqualToAnchor(tableView.centerYAnchor).active = true
            self.noPostView.anchor(self.view.topAnchor, left: nil, bottom: nil, right: nil, topConstant: 80, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.view.frame.width, heightConstant: self.noPostLabel.frame.height + self.noPostText.frame.height)

            self.noPostLabel.anchor(self.view.topAnchor, left: nil, bottom: nil, right: nil, topConstant: 80, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.noPostView.frame.width, heightConstant: 50)
            self.noPostLabel.centerXAnchor.constraintEqualToAnchor(self.noPostView.centerXAnchor).active = true


            self.noPostText.anchor(self.noPostLabel.bottomAnchor, left: self.noPostView.leftAnchor, bottom: nil, right: self.noPostView.rightAnchor, topConstant: -20, leftConstant: 35, bottomConstant: 0, rightConstant: 35)
            self.noPostText.heightAnchor.constraintEqualToConstant(60).active = true
            self.noPostText.centerXAnchor.constraintEqualToAnchor(self.noPostView.centerXAnchor).active = true

            self.loadingSpinner.stopAnimating()
        } else {
            print("THERE ARE: \(self.updates.count) POSTS")

            self.tableView.willRemoveSubview(self.noPostView)
            self.tableView.willRemoveSubview(self.noPostText)
            self.tableView.willRemoveSubview(self.noPostLabel)

        }

【问题讨论】:

  • 只在不需要标签时隐藏它们,即“self.noPostLabel.isHidden = true”,并在需要时显示它们。
  • 工作就像一个魅力@DrPatience :D 愚蠢的我........
  • 试试这个self.noPostLabel.removeFromSuperview(),希望对你有帮助
  • :) 这发生在每个人身上。生病添加我的评论作为答案。请投票。谢谢。

标签: ios iphone swift uiview uilabel


【解决方案1】:

只需在不需要标签时隐藏它们,即“self.noPostLabel.isHidden = true”,并在需要时显示它们。

【讨论】:

    【解决方案2】:

    您正在使用苹果在下面描述的willRemoveSubview(_:)

    这个方法的默认实现什么都不做。子类可以 覆盖它以在子视图出现时执行其他操作 删除。当 subview 的 superview 改变或 当子视图完全从视图层次结构中移除时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多