Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/574bc43477c2e8b3cc5be8eec37b9f5c to your computer and use it in GitHub Desktop.
Save laevandus/574bc43477c2e8b3cc5be8eec37b9f5c to your computer and use it in GitHub Desktop.
import UIKit
final class FittingHeightCollectionViewController: UICollectionViewController {
private let items: [[String]]
init(items: [[String]]) {
self.items = items
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
layout.itemSize = UICollectionViewFlowLayout.automaticSize
layout.sectionInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
super.init(collectionViewLayout: layout)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private static let reuseIdentifier = "Cell"
private var collectionViewHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(TextCollectionViewCell.self, forCellWithReuseIdentifier: Self.reuseIdentifier)
collectionView.backgroundColor = UIColor(white: 0.9, alpha: 1)
collectionView.isScrollEnabled = false
collectionViewHeightConstraint = collectionView.heightAnchor.constraint(equalToConstant: 50)
collectionViewHeightConstraint.isActive = true
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionViewHeightConstraint.constant = collectionViewLayout.collectionViewContentSize.height
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items[section].count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Self.reuseIdentifier, for: indexPath) as! TextCollectionViewCell
cell.label.text = items[indexPath.section][indexPath.item]
return cell
}
}
final class TextCollectionViewCell: UICollectionViewCell {
let label = UILabel(frame: .zero)
override init(frame: CGRect) {
super.init(frame: frame)
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 12),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -12),
label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -12)
])
backgroundView = UIView(frame: .zero)
backgroundView?.backgroundColor = .systemBlue
backgroundView?.layer.cornerRadius = 8
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// Example usage:
class ViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
let items = (0..<2).map { section in
(0..<6).map({ "Item: \(section)-\($0)" })
}
let collectionViewController = FittingHeightCollectionViewController(items: items)
collectionViewController.view.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(collectionViewController.view)
addChild(collectionViewController)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment