我有一个聊天应用程序,并且聊天collectionView是在我无权更改的pod文件中创建的。每个聊天消息都是collectionView中的一部分。我可以通过调用myCollectionView.numberOfSections确定部分(聊天消息)的数量。
我想确定collectionView中每个部分的高度。在不使用collectionView协议方法的情况下,如何以编程方式进行此操作。
您可以尝试这种方式:
class ThirdPartyViewController: UIViewController {
var collectionView: UICollectionView!
}
class YourViewController: UIViewController, UICollectionViewDelegateFlowLayout {
let vc = ThirdPartyViewController()
var collectionView: UICollectionView { return vc.collectionView }
var originDelegate: UICollectionViewDelegateFlowLayout { return vc as! UICollectionViewDelegateFlowLayout }
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//return custom value
return CGSize(width: 100, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//return origin value
return originDelegate.collectionView!(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
}
}