Skip to content

Instantly share code, notes, and snippets.

@0del
Last active March 24, 2021 11:59
Show Gist options
  • Save 0del/2237dc2081aa42eb053baaf6e4151162 to your computer and use it in GitHub Desktop.
Save 0del/2237dc2081aa42eb053baaf6e4151162 to your computer and use it in GitHub Desktop.
Use UIViewController as UICollectionViewCell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmtyCell.name, for: indexPath)
var viewController = YourViewController()
cell.contentView.addSubview(viewController.view)
viewController.view.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addConstraint(NSLayoutConstraint(item: viewController.view!, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: viewController.view!, attribute: .trailing, relatedBy: .equal, toItem: cell.contentView, attribute: .trailing, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: viewController.view!, attribute: .top, relatedBy: .equal, toItem: cell.contentView, attribute: .top, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: viewController.view!, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
viewController.didMove(toParent: self)
viewController.view.layoutIfNeeded()
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment