2017-08-11 124 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 
     } 
    } 
}