【问题标题】:Programmatically Get Height of Collection View Section Outside Collection View Protocol Methods以编程方式获取集合视图部分在集合视图协议方法之外的高度
【发布时间】:2020-05-08 01:25:40
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout


    【解决方案1】:

    你可以试试这个方法:

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2016-08-08
      • 2019-01-18
      • 2014-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      相关资源
      最近更新 更多