以编程方式在集合视图协议方法之外获取集合视图部分的高度

问题描述 投票:0回答:1

我有一个聊天应用程序,并且聊天collectionView是在我无权更改的pod文件中创建的。每个聊天消息都是collectionView中的一部分。我可以通过调用myCollectionView.numberOfSections确定部分(聊天消息)的数量。

我想确定collectionView中每个部分的高度。在不使用collectionView协议方法的情况下,如何以编程方式进行此操作。

ios swift uicollectionview uicollectionviewlayout
1个回答
0
投票

您可以尝试这种方式:

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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.