1. Collection View Cell 크기를 컨텐츠에 따라 바뀌게 하기

Untitled

1) 임시라벨을 사용해서 크기를 지정한다!

임시로 컨텐츠가 들어간 라벨을 만들기

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)
    }

2) 텍스트 사이즈 만큼 크기 부여하기

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 크기를 컨텐츠에 따라 바뀌게 하기

2. estimatedRowHieght

// 프로퍼티 사용
tableView.estimatedRowHeight = 42.0

// 메소드 사용func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 42.0
}

3. automaticDimension

// 프로퍼티 대입
tableView.rowHeight = UITableView.automaticDimension

// 메소드 사용func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

TableView 동적인 셀 높이 조절

[iOS - swift] self resizing cell, dynamic cell, flexible cell (cell크기에 따라 자동으로 cell height 조정, stackView 이용)