임시로 컨텐츠가 들어간 라벨을 만들기
그 라벨사이즈로 셀 크기 부여하기
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let tmpLabel : UILabel = UILabel()
tmpLabel.text = HomeNewCategoryModel.homeNewCategoryData[indexPath.item].name
return CGSize(width: Int(tmpLabel.intrinsicContentSize.width) + 30, height: 35)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: HomeNewCategoryModel.homeNewCategoryData[indexPath.item].name.size(withAttributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14)]).width + 20, height: 32)
}
[iOS] CollectionView Cell 너비 동적으로 조절하기
[iOS] Collection View Cell 크기를 컨텐츠에 따라 바뀌게 하기
// 프로퍼티 사용
tableView.estimatedRowHeight = 42.0
// 메소드 사용func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 42.0
}
// 프로퍼티 대입
tableView.rowHeight = UITableView.automaticDimension
// 메소드 사용func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}