Skip to content

Instantly share code, notes, and snippets.

@byJeevan
Created July 21, 2018 18:13
Show Gist options
  • Save byJeevan/133231bd089a09fef87ed619d0d39428 to your computer and use it in GitHub Desktop.
Save byJeevan/133231bd089a09fef87ed619d0d39428 to your computer and use it in GitHub Desktop.
//Custom class
class ExclusiveCollectionView : UIView, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var exCollectionView: UICollectionView!
//Inits
class func instanceFromNib() -> ExclusiveCollectionView {
return UINib(nibName: "ExclusiveCollectionView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ExclusiveCollectionView
}
override func awakeFromNib() {
self.prepare()
}
private func prepare() {
self.exCollectionView.delegate = self
self.exCollectionView.dataSource = self
self.exCollectionView.register(UINib.init(nibName: "ExclusiveCell", bundle: nil), forCellWithReuseIdentifier: "ExclusiveCell")
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0.0
layout.minimumInteritemSpacing = 0.0
layout.itemSize = CGSize.init(width: self.exCollectionView.frame.size.width/3.0, height: self.exCollectionView.frame.size.height/2.0) // TODO : Fix the item size distribution problem,
layout.invalidateLayout()
self.exCollectionView.collectionViewLayout = layout
}
//MARK:- Collection view delegate
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExclusiveCell", for: indexPath) as! ExclusiveCell
cell.testLabel.text = "Item : \(indexPath.row)"
cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.red.cgColor
return cell
}
}
//Parent
class ViewController: UIViewController {
@IBOutlet weak var myStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
let customView = ExclusiveCollectionView.instanceFromNib()
self.myStackView.addArrangedSubview(customView)
// self.customView?.setNeedsLayout()
// self.customView?.setNeedsDisplay()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment