Created
October 29, 2020 08:05
-
-
Save NohEunTae/cc8bd3becf0c67fe8b41a87f6a98c992 to your computer and use it in GitHub Desktop.
DynamicCellSizing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// CollectionViewPractice | |
// | |
// Created by nTom on 2020/10/26. | |
// Copyright © 2020 CollectionViewPractice. All rights reserved. | |
// | |
import UIKit | |
import SnapKit | |
final class ViewController: UIViewController { | |
@IBOutlet private weak var collectionView: UICollectionView! | |
private let items: [String] = [ | |
"또띠아", | |
"버터", | |
"달걀", | |
"치즈볼", | |
"쪽파", | |
"진미채", | |
"참외", | |
"케이크", | |
"새우", | |
"대패삼겹살", | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupView() | |
} | |
private func setupView() { | |
view.backgroundColor = .white | |
setupCollectionView() | |
} | |
private func setupCollectionView() { | |
let flowLayout = UICollectionViewFlowLayout() | |
flowLayout.minimumLineSpacing = .zero | |
flowLayout.minimumInteritemSpacing = 16 | |
flowLayout.scrollDirection = .horizontal | |
flowLayout.sectionInset = .init(top: 5, left: 16, bottom: 5, right: 16) | |
collectionView.setCollectionViewLayout(flowLayout, animated: false) | |
collectionView.delegate = self | |
collectionView.dataSource = self | |
collectionView.backgroundColor = .white | |
collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell") | |
} | |
} | |
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return items.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell | |
cell.configure(name: items[indexPath.item]) | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CollectionViewCell.fittingSize(availableHeight: 45, name: items[indexPath.item]) | |
} | |
} | |
final class CollectionViewCell: UICollectionViewCell { | |
static func fittingSize(availableHeight: CGFloat, name: String?) -> CGSize { | |
let cell = CollectionViewCell() | |
cell.configure(name: name) | |
let targetSize = CGSize(width: UIView.layoutFittingCompressedSize.width, height: availableHeight) | |
return cell.contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: .fittingSizeLevel, verticalFittingPriority: .required) | |
} | |
private let titleLabel: UILabel = UILabel() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupView() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
setupView() | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
layer.cornerRadius = frame.height / 2 | |
} | |
private func setupView() { | |
backgroundColor = .black | |
titleLabel.textAlignment = .center | |
titleLabel.textColor = .white | |
contentView.addSubview(titleLabel) | |
titleLabel.snp.makeConstraints { (make) in | |
make.edges.equalToSuperview().inset(15) | |
} | |
} | |
func configure(name: String?) { | |
titleLabel.text = name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment