Search code examples
iosswiftcollectionview

Get rid of subview if collection view is empty


I have a collection view when I display a message to the user if the collection view is empty. I also programmatically add a button to let the user add a photo if the collection view is empty. However, once the photo is added and the I reload the collection view, I found a way to get rid of the label, however, how can I get rid of the button's subview? since I can't access "self.collectionViews?.addSubview(button)" since it is in a guard let statement. Thanks in advance!

  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

Solution

  • I would recommend making a UIView that contains both your UILabel and the UIButton. Then setting the aforementioned UIView as the backgroundView of the UICollectionView. Ensure userInteraction is enabled on the backgroundView.

    This will make sure that the both the UILabel & the UIButton are removed when you set the backgroundView to nil.

    Something like this should do the trick (Be aware I have not tested this):

        //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
    

    Then in the future when you want to remove both elements you can just set the backgroundView to nil.