【问题标题】:Get rid of subview if collection view is empty如果集合视图为空,则摆脱子视图
【发布时间】:2017-08-14 01:21:54
【问题描述】:

如果集合视图为空,当我向用户显示消息时,我有一个集合视图。如果集合视图为空,我还以编程方式添加一个按钮,让用户添加照片。但是,一旦添加了照片并且我重新加载了集合视图,我找到了一种摆脱标签的方法,但是,我怎样才能摆脱按钮的子视图?因为我无法访问“self.collectionViews?.addSubview(button)”,因为它位于 guard let 语句中。提前致谢!

  guard let parseUSERS = parseJSON["users"] as? [AnyObject] else {


// if the collection view is empty, display a message
   let messageLabel = UILabel(frame: CGRect(x: 20.0, y: 10, width: self.collectionViews!.bounds.size.width - 40.0, height: (self.collectionViews?.bounds.size.height)!))
   messageLabel.text = "Collection view is empty!"
   messageLabel.font = messageLabel.font.withSize(20)
   messageLabel.font = UIFont.boldSystemFont(ofSize: messageLabel.font.pointSize)
   messageLabel.textColor = UIColor.white
   messageLabel.numberOfLines = 0
   messageLabel.textAlignment = NSTextAlignment.center
   messageLabel.sizeToFit()

   let button = UIButton(frame: CGRect(x: 80.0, y: 320, width: 215, height: 50))
   button.backgroundColor = UIColor.white
   button.setTitle("create",for: .normal)
   button.setTitleColor(colorCircleBlue, for: .normal)
   button.addTarget(self, action: #selector(self.action(sender:)), for: .touchUpInside)

   // round corners for login/register buttons
   button.layer.cornerRadius = button.bounds.width / 20

   self.collectionViews?.backgroundColor = UIColor.blue
   self.collectionViews?.backgroundView = messageLabel
   self.collectionViews?.addSubview(button)


    return
}


    self.collectionViews?.backgroundColor = UIColor.white
    self.collectionViews?.backgroundView = nil

【问题讨论】:

    标签: ios swift collectionview


    【解决方案1】:

    我建议制作一个同时包含您的 UILabel 和 UIButton 的 UIView。然后将前面提到的 UIView 设置为 UICollectionView 的 backgroundView。确保在 backgroundView 上启用了 userInteraction。

    当您将 backgroundView 设置为 nil 时,这将确保 UILabel 和 UIButton 都被删除。

    这样的事情应该可以解决问题(请注意我还没有测试过):

        //Container view
        let view = UIView.init(frame: self.collectionViews?.frame)
        //Label 
        let messageLabel = UILabel(frame: CGRect(x: 20.0, y: 10, width: self.collectionViews!.bounds.size.width - 40.0, height: (self.collectionViews?.bounds.size.height)!))
        messageLabel.text = "Collection view is empty!"
        messageLabel.font = messageLabel.font.withSize(20)
        messageLabel.font = UIFont.boldSystemFont(ofSize: messageLabel.font.pointSize)
        messageLabel.textColor = UIColor.white
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = NSTextAlignment.center
        messageLabel.sizeToFit()
        //Button
        let button = UIButton(frame: CGRect(x: 80.0, y: 320, width: 215, height: 50))
        button.backgroundColor = UIColor.white
        button.setTitle("create",for: .normal)
        button.setTitleColor(colorCircleBlue, for: .normal)
        button.addTarget(self, action: #selector(self.action(sender:)), for: .touchUpInside)
        // round corners for login/register buttons
        button.layer.cornerRadius = button.bounds.width / 20
        //Add elements to container view
        view.addSubview(messageLabel)
        view.addSubview(button)
        self.collectionViews?.backgroundView = view
    

    以后当你想删除这两个元素时,你可以将 backgroundView 设置为 nil。

    【讨论】: