Skip to content

Commit

Permalink
[Design] #319 - 메뉴명 2줄 이상일 경우를 대비해 동적 레이아웃 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
EunsuSeo01 committed Dec 29, 2024
1 parent 1bbbe3f commit 0b53a6b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UIKit

final class HankkiMenuCollectionViewCell: BaseCollectionViewCell {

// MARK: - Properties
// MARK: - UI Components

private var nameLabel: UILabel = UILabel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ private extension HankkiDetailViewController {
)
detailMapView.bindData(latitude: data.latitude, longitude: data.longitude)

menuCollectionView.updateLayout(menuSize: data.menus.count)
menuCollectionView.collectionView.reloadData()
}
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ 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

override func setupHierarchy() {
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)
Expand All @@ -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()
}
}

0 comments on commit 0b53a6b

Please sign in to comment.