我已经以编程方式制作了一个UIView和两个UILabel。标签上写着这样的内容:“没有帖子,跟随某人添加到您的Feed中。”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”,并在需要时显示它们。
关于ios - 添加后以编程方式删除UILabel-Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40934659/