添加 UICollectionViewDelegateFlowLayout
代理
实现下面的代理方法
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let folderName = decoFolder[indexPath.row].name
let attributes = [NSFontAttributeName : UIFont.systemFontOfSize(15.0)]
let rect: CGRect = folderName.boundingRectWithSize(CGSize(width: CGFloat.max, height: 50), options: NSStringDrawingOptions.UsesFontLeading, attributes: attributes, context: nil)
var size: CGSize
if rect.size.width < 50.0 {
size = CGSize(width: 50.0, height: folderCollectionView.frame.height)
} else {
size = CGSize(width: rect.size.width + 12.0, height: folderCollectionView.frame.height)
}
return size
}