From 0b53a6ba3087f42508955910f4b86fa7db737352 Mon Sep 17 00:00:00 2001 From: EunsuSeo01 Date: Mon, 30 Dec 2024 07:40:16 +0900 Subject: [PATCH] =?UTF-8?q?[Design]=20#319=20-=20=EB=A9=94=EB=89=B4?= =?UTF-8?q?=EB=AA=85=202=EC=A4=84=20=EC=9D=B4=EC=83=81=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=EB=A5=BC=20=EB=8C=80=EB=B9=84=ED=95=B4=20?= =?UTF-8?q?=EB=8F=99=EC=A0=81=20=EB=A0=88=EC=9D=B4=EC=95=84=EC=9B=83=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cell/HankkiMenuCollectionViewCell.swift | 1 - .../View/HankkiDetailViewController.swift | 22 ++++++++++++++++++- .../View/HankkiMenuCollectionView.swift | 20 ++++++++++------- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift index fa53bf31..e2e0a0f5 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift @@ -9,7 +9,6 @@ import UIKit final class HankkiMenuCollectionViewCell: BaseCollectionViewCell { - // MARK: - Properties // MARK: - UI Components private var nameLabel: UILabel = UILabel() diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift index 5d626eec..611a0c56 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift @@ -171,7 +171,6 @@ private extension HankkiDetailViewController { ) detailMapView.bindData(latitude: data.latitude, longitude: data.longitude) - menuCollectionView.updateLayout(menuSize: data.menus.count) menuCollectionView.collectionView.reloadData() } } @@ -370,10 +369,31 @@ extension HankkiDetailViewController: UICollectionViewDataSource, UICollectionVi func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HankkiMenuCollectionViewCell.className, for: indexPath) as? HankkiMenuCollectionViewCell else { return UICollectionViewCell() } + if let data = viewModel.hankkiDetailData { cell.bindData(data.menus[indexPath.item]) + menuCollectionView.updateLayout() return cell } return UICollectionViewCell() } } + +extension HankkiDetailViewController: UICollectionViewDelegateFlowLayout { + + // 메뉴명 label 길이에 따라 다르게 셀 크기 지정 + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + if let data = viewModel.hankkiDetailData { + let menuNameLabelSize = NSString(string: data.menus[indexPath.item].name) + .boundingRect( + with: CGSize(width: UIScreen.getDeviceWidth() - 44, height: CGFloat.greatestFiniteMagnitude), + options: .usesLineFragmentOrigin, + attributes: [NSAttributedString.Key.font: UIFont.setupPretendardStyle(of: .body8)], + context: nil + ) + return CGSize(width: UIScreen.getDeviceWidth(), height: menuNameLabelSize.height + 25) + } + + return .zero + } +} diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift index e5cda280..25ecc0b0 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift @@ -12,7 +12,7 @@ final class HankkiMenuCollectionView: BaseView { // MARK: - UI Components private let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() - lazy var collectionView: UICollectionView = UICollectionView(frame: .init(x: 0, y: 0, width: UIScreen.getDeviceWidth(), height: 179), collectionViewLayout: flowLayout) + lazy var collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) // MARK: - Life Cycle @@ -20,9 +20,16 @@ final class HankkiMenuCollectionView: BaseView { addSubview(collectionView) } + override func setupLayout() { + collectionView.snp.makeConstraints { + $0.edges.equalToSuperview() + $0.width.equalTo(UIScreen.getDeviceWidth()) + $0.height.equalTo(179) + } + } + override func setupStyle() { flowLayout.do { - $0.itemSize = .init(width: UIScreen.getDeviceWidth(), height: 45) $0.scrollDirection = .vertical $0.minimumLineSpacing = 24 $0.headerReferenceSize = .init(width: UIScreen.getDeviceWidth(), height: 20 + 26) @@ -39,14 +46,11 @@ final class HankkiMenuCollectionView: BaseView { extension HankkiMenuCollectionView { - /// 메뉴 데이터 불러온 이후에 메뉴 개수에 따라 높이 동적으로 설정 - func updateLayout(menuSize: Int) { - collectionView.snp.removeConstraints() - collectionView.snp.makeConstraints { + func updateLayout() { + collectionView.snp.updateConstraints { $0.edges.equalToSuperview() $0.width.equalTo(UIScreen.getDeviceWidth()) - $0.height.equalTo(20 + 26 + 18 + (menuSize * (45 + 24)) + 48 + 18) + $0.height.equalTo(collectionView.contentSize.height) } - layoutIfNeeded() } }