Last active
March 24, 2021 11:59
-
-
Save 9bany/2237dc2081aa42eb053baaf6e4151162 to your computer and use it in GitHub Desktop.
Use UIViewController as UICollectionViewCell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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