How to determine height of UICollectionView with FlowLayout

前端 未结 13 2041
醉话见心
醉话见心 2020-11-28 18:01

I\'ve got an UICollectionView with an UICollectionViewFlowLayout, and i want to calculate its content size (for return in intrinsicContentSiz

相关标签:
13条回答
  • 2020-11-28 18:33

    Whoa! For some reason, after hours of research, I now found a pretty easy answer to my question: I was completely searching in the wrong place, digging through all the documentation I could find on UICollectionView.

    The simple and easy solution lies in the underlying layout: Just call collectionViewContentSize on your myCollectionView.collectionViewLayout property and you get the height and width of the content as CGSize. It's as easy as that.

    0 讨论(0)
  • 2020-11-28 18:33

    Swift 4

    class DynamicCollectionView: UICollectionView {
      override func layoutSubviews() {
        super.layoutSubviews()
        if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
          self.invalidateIntrinsicContentSize()
        }
      }
    
      override var intrinsicContentSize: CGSize {
        return collectionViewLayout.collectionViewContentSize
      }
    }
    
    0 讨论(0)
  • 2020-11-28 18:34

    user1046037 answer in Swift...

    class DynamicCollectionView: UICollectionView {
        override func layoutSubviews() {
            super.layoutSubviews()
            if bounds.size != intrinsicContentSize() {
                invalidateIntrinsicContentSize()
            }
        }
    
        override func intrinsicContentSize() -> CGSize {
            return self.contentSize
        }
    }
    
    0 讨论(0)
  • 2020-11-28 18:37

    In case you are using Auto layout, then you could create a subclass of UICollectionView

    If you use the below the code then you don't have to specify any height constraints for the collection view as it would vary based on the contents of the collection view.

    Given below is the implementation:

    @interface DynamicCollectionView : UICollectionView
    
    @end
    
    @implementation DynamicCollectionView
    
    - (void) layoutSubviews
    {
        [super layoutSubviews];
    
        if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize]))
        {
            [self invalidateIntrinsicContentSize];
        }
    }
    
    - (CGSize)intrinsicContentSize
    {
        CGSize intrinsicContentSize = self.contentSize;
    
        return intrinsicContentSize;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-28 18:41

    Swift 3 code for user1046037 answer

    import UIKit
    
    class DynamicCollectionView: UICollectionView {
    
        override func layoutSubviews() {
            super.layoutSubviews()
            if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
                self.invalidateIntrinsicContentSize()
            }
    
        }
    
        override var intrinsicContentSize: CGSize {
            return contentSize
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 18:42

    This answer is based on @Er. Vihar's answer. You can easily implement the solution by using RxSwift

         collectionView.rx.observe(CGSize.self , "contentSize").subscribe(onNext: { (size) in
            print("sizer \(size)")
        }).disposed(by: rx.disposeBag)
    
    0 讨论(0)
提交回复
热议问题