lazy var collectView: UICollectionView = {
let layout = PageCollectionViewLayout.init()
layout.itemSize = CGSize(width: screenWidth, height: screenHeight - iPhoneTabBarHeight())
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
// layout.footerReferenceSize = CGSize(width: screenWidth, height: 30)
// layout.headerReferenceSize = CGSize(width: screenWidth, height: 0)
let collectView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight - iPhoneTabBarHeight()), collectionViewLayout: layout)
collectView.backgroundColor = .clear
collectView.delegate = self
collectView.dataSource = self
collectView.showsVerticalScrollIndicator = false
collectView.showsHorizontalScrollIndicator = false
collectView.isPagingEnabled = true
collectView.bounces = false
// collectView.register(SwiftFooterCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: “SwiftFooterCollectionReusableView”)
collectView.register(UINib.init(nibName: “CarOrderListCell”, bundle: nil), forCellWithReuseIdentifier: “CarOrderListCell”)
// collectView.register(CarOrderListCell.classForCoder(), forCellWithReuseIdentifier: “CarOrderListCell”)
//返回的是 UICollectionReusableView
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: “kHeaderViewID”)
//返回的是xib
collectionView.register(UINib(nibName: “CollectionHeaderView”, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: “kHeaderViewID”)
return collectView
}()
//设置 区头和区尾
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let headerView:SwiftHeaderCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: “SwiftHeaderCollectionReusableView”, for: indexPath) as! SwiftHeaderCollectionReusableView
return headerView
}
else
{
let footerView:SwiftFooterCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftFooterCollectionReusableView", for: indexPath) as! SwiftFooterCollectionReusableView
return footerView
}
}
Swift实现自动计算collectionView的item的大小
实现步骤:
1)遵守代理UICollectionViewDelegateFlowLayout
2)实现UICollectionViewDelegateFlowLayout代理
在下面的代理方法中实现自动计算文本宽度即可!!!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let str = titleArr[indexPath.row]
let dic = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13.0)]
let size = CGSize(width: CGFloat(MAXFLOAT), height: 30)
let width = str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic, context: nil).size.width
return CGSize(width: width+20, height: 30)
}