UICollectionView in Swift
var cvMenu:UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
var layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
/////Create Frame
cvMenu = UICollectionView(frame: CGRect (x: 5, y: 0, width: Constants.ScreenWidth-10, height: Constants.ScreenHeight-130), collectionViewLayout: layout)
cvMenu.dataSource = self
cvMenu.delegate = self
//Register Nib File if custom nib
let nibName = UINib(nibName: "CVDashboard", bundle:nil)
cvMenu.register(nibName, forCellWithReuseIdentifier: "CVCell");
cvMenu.showsVerticalScrollIndicator = false
cvMenu.backgroundColor = UIColor.clear;
self.addSubview(cvMenu)
//MARK: - Collection View Delegates
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVCell", for: indexPath as IndexPath)
cell.backgroundColor = colorMenu.object(at: indexPath.row) as? UIColor
let lblTitle:UILabel = (cell.viewWithTag(101) as? UILabel)!
return cell
}
func collectionView(_ collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
// let cellSize:CGSize = CGSize(250 250);
let cellWidth:Float = Float((cvMenu.frame.size.width-10)/2);
let cellHeight:Float = Float(cellWidth/4*2.5);
if (indexPath.row == 4) {
return CGSize(width: Int(cvMenu.frame.size.width), height: Int(cellHeight))
}
else {
return CGSize(width: Int(cellWidth), height: Int(cellHeight))
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForItemAt indexPath: IndexPath) -> UIEdgeInsets {
//return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
return UIEdgeInsetsMake(0, 10, 0, 10);
}