Swift/UIKit

[UIKit] collectionView를 dynamic height로 구현해보자

insub4067 2022. 7. 22. 00:51
final class UserViewController: UIViewController {

    private let feedCollectionView: UICollectionView = {

        let surfaceLength = UIScreen.main.bounds.width / 3

        let layout = UICollectionViewFlowLayout()
        layout.estimatedItemSize = CGSize(width: surfaceLength, height: surfaceLength)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.register(UserViewFeedCollectionViewCell.self, forCellWithReuseIdentifier: UserViewFeedCollectionViewCell.identifier)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.isScrollEnabled = false
        
        return collectionView
    }()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        feedCollectionView.heightAnchor.constraint(equalToConstant: feedCollectionView.collectionViewLayout.collectionViewContentSize.height).isActive = true
    }
    
}