2017-08-11 140 views
0

如果集合視圖爲空,如何刪除我的集合視圖標題?我能夠以編程方式添加標籤,如果集合視圖在我的集合視圖文件中爲空,但我似乎無法找到如何刪除標題。收集視圖爲空時刪除集合視圖標題

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 = "No info yet" 
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() 

self.collectionViews?.backgroundColor = blue 
self.collectionViews?.backgroundView = messageLabel 

回答

0

我認爲你可以做到這一點是這樣的:

extension ViewController: UICollectionViewDelegateFlowLayout { 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 

     if collectionView.numberOfItems(inSection: section) == 0 { 
      return CGSize.zero 
     } else { 
      let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView 
      let existingSize = headerView.frame.size 

      return existingSize 
     } 
    } 
}