๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS/UI] ์ปฌ๋ ‰์…˜๋ทฐ์…€ ์ž๋™์œผ๋กœ ํฌ๊ธฐ ์กฐ์ •ํ•˜๊ณ  ์™ผ์ชฝ ์ •๋ ฌํ•˜๊ธฐ (CollectionViewCell Automaticsize & LeftAlign)

by Fomagran ๐Ÿ’ป 2021. 6. 29.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” 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

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€