swift collectView

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)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值