[iOS/UI] ์ปฌ๋ ์ ๋ทฐ์ ์๋์ผ๋ก ํฌ๊ธฐ ์กฐ์ ํ๊ณ ์ผ์ชฝ ์ ๋ ฌํ๊ธฐ (CollectionViewCell Automaticsize & LeftAlign)
์๋ ํ์ธ์ Foma ๐ ์ ๋๋ค.
์ค๋์ ์ ์ ์ผ์ชฝ์ผ๋ก ์ ๋ ฌ์ํค๊ณ ์ ์์ ์๋ ํ ์คํธ์ ๋ฐ๋ผ์ ์๋์ผ๋ก ํฌ๊ธฐ๊ฐ ์กฐ์ ๋๋ ๊ฒ์ ์์๋ณด๋๋ก ํ ๊ฒ์!
(์ผ๋ฐ์ ์ผ๋ก ์ปฌ๋ ์ ๋ทฐ์ ์ ๋ง๋ค๋ฉด ์๋์ ๊ฐ์ด ๋ ๊ฑฐ์์!)
์ค๋์ ์๋์ ๊ฐ์ด ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
๋ฐ๋ก ์์ํ ๊ฒ์~
StoryBoard
์คํ ๋ฆฌ๋ณด๋๋ ์๋์ ๊ฐ์ด ์ค์ ํด์ฃผ์ธ์.
CollectionViewCell
์ปฌ๋ ์ ๋ทฐ์ ์ ๋ ์ด๋ธ์ ์ฐ๊ฒฐํด์ค๋๋ค.
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
ViewController
๋ทฐ์ปจํธ๋กค๋ฌ์ ์ปฌ๋ ์ ๋ทฐ๋ฅผ ์ฐ๊ฒฐํด์ค๋๋ค.
@IBOutlet weak var collection: UICollectionView!
์ปฌ๋ ์ ๋ทฐ ๋๋ฆฌ๊ฒ์ดํธ์ ๋ฐ์ดํฐ์์ค๋ฅผ ํ์ฌ ๋ทฐ์ปจํธ๋กค๋ฌ๋ก ์ค์ ํด์ฃผ์ธ์.
func configure() {
collection.delegate = self
collection.dataSource = self
}
๊ธ์๋ฅผ ํ์ํ letters ๋ฐฐ์ด๋ ๋ง๋ค์ด์ค๋๋ค.
let letters = ["์๋
","์๋
ํ์ธ์","์๋
ํ์ธ์ ์ ๋ ํฌ๋ง์
๋๋ค.","์๋
ํ์ธ์ ๋ง๋์ ์ ๋ง ๋ฐ๊ฐ์ต๋๋ค."]
๋๋ฆฌ๊ฒ์ดํธ์ ๋ฐ์ดํฐ์์ค๋ฅผ ์๋์ ๊ฐ์ด ์ค์ ํด์ฃผ์ธ์!
extension ViewController:UICollectionViewDelegate {
}
extension ViewController:UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return letters.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collection.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.label.text = letters[indexPath.item]
return cell
}
}
CollectionViewLeftAlignFlowLayout
์ด๋ฒ ๊ธ์ ํต์ฌ์ธ ์ผ์ชฝ์ผ๋ก ์ ๋ ฌ๋๋ ํ๋ก์ฐ๋ ์ด์์ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
class CollectionViewLeftAlignFlowLayout: UICollectionViewFlowLayout {
let cellSpacing: CGFloat = 10
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
self.minimumLineSpacing = 10.0
self.sectionInset = UIEdgeInsets(top: 12.0, left: 16.0, bottom: 0.0, right: 16.0)
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
}
layoutAttribute.frame.origin.x = leftMargin
leftMargin += layoutAttribute.frame.width + cellSpacing
maxY = max(layoutAttribute.frame.maxY, maxY)
}
return attributes
}
}
ViewController
๋ค์ ๋ทฐ์ปจํธ๋กค๋ฌ๋ก ๋์์์
์ปฌ๋ ์ ๋ทฐ์ ์ปฌ๋ ์ ๋ทฐ๋ ์ด์์์ ์์์ ๋ง๋ค์ด์ค CollectionViewLeftAlignFlowLayout์ผ๋ก ์ค์ ํด์ฃผ์ธ์.
collection.collectionViewLayout = CollectionViewLeftAlignFlowLayout()
์ด์ ์ฑ์ ์คํ์ํค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋์ฌ๊ฑฐ์์.
์ด๋ ๊ฒ ๋์ค๋ ์ด์ ๋ ํ๋ก์ฐ ๋ ์ด์์์์ ์ ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํด์ฃผ์ง ์์๊ธฐ ๋๋ฌธ์ธ๋ฐ์.
์๋์ ๊ฐ์ด flowLayout์ estimatedItemSize๋ฅผ UICollectonViewFlowLayout.automaticSize๋ก ์ง์ ํฉ๋๋ค.
if let flowLayout = collection?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
๋ฐ๋์ ์์์ ๋ง๋ค์ด์ค CollectionViewLeftAlignFlowLayout ์ธ์คํด์ค๋ฅผ ์์ฑํด์ค ๋ค ์ค์ ํด์ฃผ์ ์ผ ํฉ๋๋ค.
collection.collectionViewLayout = CollectionViewLeftAlignFlowLayout()
if let flowLayout = collection?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
์ด๋ ๊ฒ ์คํํ๋ฉด ์ฌ์ด์ฆ๋ ์๋์ผ๋ก ์กฐ์ ์ด ๋๊ณ ์ผ์ชฝ์ผ๋ก ์ ๋ ฌ๋ ์ปฌ๋ ์ ๋ทฐ ์ ์ ๋ง๋ค ์ ์์ต๋๋ค!
Source Code
์ค๋์ ์ปฌ๋ ์ ๋ทฐ ์ ์ ์ฌ์ด์ฆ๋ฅผ ์๋์ผ๋ก ์กฐ์ ํ๊ณ ์ผ์ชฝ์ผ๋ก ์ ๋ ฌํ๋ ๋ฒ์ ๋ํด์ ์์๋ณด์์ต๋๋ค.
ํน์๋ผ๋ ๊ถ๊ธํ์ ์ ์ด๋ ํ๋ฆฐ ์ ์ด ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!
Reference
iOS Tutorial: Left-align CollectionView Cell | DaddyCoding - Learn Swift, SwiftUI, iOS, RxSwift Programming
This design is heavily used on the search screen where you would like to show some of the recent searches made by users or some of the popular searches on your
daddycoding.com