0
프로그래밍 방식으로 UICollectionView
가로 스크롤을 만들었습니다. UICollectionView
셀 너비는 내용에 따라 동적으로 생성됩니다.UICollectionView 동적 셀 겹침 문제
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CustomCollectionViewCell
cell.label.text = dataSource[indexPath.item]
cell.label.font = UIFont(name: "Helvetica", size: 20.0)
if currentPageIndex == indexPath.item {
cell.currentPageIndicator.frame = CGRect(x: 0, y: cell.bounds.height - 2, width: cell.bounds.width, height: 2.5)
cell.currentPageIndicator.isHidden = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let size = cellSizeCache.object(forKey: indexPath as AnyObject) as? NSValue {
return size.cgSizeValue
}
let string = dataSource[indexPath.item]
let height = collectionView.frame.size.height
let font = UIFont(name: "Helvetica", size: 20.0)
var size = string.boundingRect(with: CGSize(width: CGFloat.infinity, height: height), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font!], context: nil).size
size.width += 20
size.height += 20
return size
}
처음에는 가로로 스크롤해도 UICollectionView
이 잘 보입니다. 빠르게 스크롤을 시작하면 셀이 서로 겹칩니다.