2017-08-14 163 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 

然後在未來:

像這樣的東西應該做的伎倆(注意我沒有測試過這一點)。