UICollectionView autosize height

前端 未结 9 1675
孤城傲影
孤城傲影 2020-12-04 07:02

How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData an

相关标签:
9条回答
  • 2020-12-04 07:23
    UICollectionViewFlowLayout *flowLayout;
    flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setMinimumInteritemSpacing:0.0f];
    [flowLayout setMinimumLineSpacing:0.0f];
    [self.collectionView setPagingEnabled:NO];
    [flowLayout setItemSize:CGSizeMake(322.0, 148.0)];  //important to leave no white space between the images
    [self.collectionView setCollectionViewLayout:flowLayout];
    

    I found that autolayout in the storyboard is not helping too much. A correct setting for the UICollectionViewFlowLayout for your collectionView is the real help. If you adjust item size with setItemSize, you may get the result you want.

    0 讨论(0)
  • 2020-12-04 07:24

    Here's my implementation in Swift 3:

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        if (self.superview != nil) {
            self.superview?.layoutIfNeeded()
        }
    
        return collectionView.contentSize
    }
    
    0 讨论(0)
  • 2020-12-04 07:27

    Here's a way to bind the CollectionView's height via it's intrinsic size. I used it to properly size a CollectionView inside a TableView Cell (with dynamic cells height). and it works perfectly.

    First, add this to your UICollectionView subclass:

    override var intrinsicContentSize: CGSize {
        get {
            return self.contentSize
        }
    }
    

    Then call layoutIfNeeded() after you reload data:

    reloadData()
    layoutIfNeeded()
    
    0 讨论(0)
  • 2020-12-04 07:27

    You can try out my custom AGCollectionView class

    • Assign a height constraint of collectionView using a storyboard or programmatically.

    - Assign this class to your UICollectionView.

    class AGCollectionView: UICollectionView {
    
        fileprivate var heightConstraint: NSLayoutConstraint!
    
        override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
            super.init(frame: frame, collectionViewLayout: layout)
            self.associateConstraints()
        }
    
        required public init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.associateConstraints()
        }
    
        override open func layoutSubviews() {
            super.layoutSubviews()
    
            if self.heightConstraint != nil {
                self.heightConstraint.constant = floor(self.contentSize.height)
            }
            else{
                self.sizeToFit()
                print("Set a heightConstraint set size to fit content")
            }
        }
    
        func associateConstraints() {
            // iterate through height constraints and identify
    
            for constraint: NSLayoutConstraint in constraints {
                if constraint.firstAttribute == .height {
                    if constraint.relation == .equal {
                        heightConstraint = constraint
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 07:34
    1. Add IBOutlet for CollectionView Height Constraint --> Like @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
    2. Add Below snipped code.

    0 讨论(0)
  • 2020-12-04 07:36

    It seems to work nicely with a custom UICollectionView class.

    class AutoSizedCollectionView: UICollectionView {
    
        override var contentSize: CGSize {
            didSet {
                invalidateIntrinsicContentSize()
            }
        }
    
        override var intrinsicContentSize: CGSize {
            layoutIfNeeded()
            return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
        }
    }
    

    Set your custom class in the interface builder:

    This way you can also set your collection views intrinsic size to 'placeholder' in interface builder to avoid having to set a height constraint.

    I hope this helps someone else.

    0 讨论(0)
提交回复
热议问题