swift - 如果 Collection View 为空,则删除 Collection View 标题

标签 swift uicollectionview

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

相关文章:

swift - 错误: Array index out of range Swift

swift - 如何将 UITableView 作为 subview 添加到 xib 中的 UIView?

SwiftUI ForEach 用于预览和打印设备名称

swift - 在 swift 中,为什么我不能实例化一个有初始化器的协议(protocol)?

ios - 如何清除Swift或Objective-C中对NSComboBox的选择?

ios - 将 UIImage 分配给 Collection View 单元格

xcode - 如何根据按下的按钮将存储在 Plist 中的 URL 传递到 WebView

iOS:自定义 View Controller 作为ivars

ios - 访问位于设备屏幕中心的 UICollectionView 单元格中的 View

ios - 如何在日历中设置默认提醒?