如果 Collection View 为空,如何删除 Collection View 标题?如果 Collection View 文件中的 Collection View 为空,我能够以编程方式添加标签,但我似乎无法找到如何删除标题。
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
最佳答案
我认为你可以这样实现:
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
}
}
}
关于swift - 如果 Collection View 为空,则删除 Collection View 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645165/