2017-08-14 39 views
0

如果集合视图为空,则向用户显示消息时我有一个集合视图。我还以编程方式添加一个按钮,让用户在收藏视图为空时添加照片。但是,一旦照片被添加并且我重新加载了收藏视图,我发现了一种摆脱标签的方式,但是,我怎样才能摆脱按钮的子视图?因为我不能访问“self.collectionViews?.addSubview(button)”,因为它在警卫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 

回答

1

我会推荐制作一个包含你的UILabel和UIButton的UIView。然后将上述UIView设置为UICollectionView的backgroundView。确保在backgroundView上启用userInteraction。

当你将backgroundView设置为nil时,这将确保UILabel & UIButton被删除。当你想删除你可以设置backgroundView为零两个元素

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

然后在未来:

像这样的东西应该做的伎俩(注意我没有测试过这一点)。