As far as I understand , You want to open custom view which is loaded from xib on your ViewControllers view or a different ViewControllers view on your ViewController(that display various item in collectionView).
If yes then use below code
//Create optional property
var myViewController : YourCustomViewController?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
if indexPath.item == 0 {
//add below line to load custom View Xib
loadCustomView(onView:self.view)
//OR To Add ViewController View use below code
loadViewController(onView:self.view)
}
}
fun loadCustomView(onView:UIView) {
let allViewsInXibArray = Bundle.init(for: type(of: self)).loadNibNamed("CustomView", owner: self, options: nil)
//If you only have one view in the xib and you set it's class to MyView class
let myCustomView = allViewsInXibArray?.first as! CustomView
onView addSubview(myCustomView)
addConstraints(onView: myCustomView)
}
func loadViewController(onView:UIView) {
myViewController = YourCustomViewController(nibName: "TestViewController", bundle: nil);
onView addSubview((myViewController?.view)!)
addConstraints(onView: (myViewController?.view)!)
}
func addConstraints(onView : UIView) {
onView.translatesAutoresizingMaskIntoConstraints = false;
let widthConstraint = NSLayoutConstraint(item: onView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal,
toItem: onView.superview, attribute: .width, multiplier: 0.8, constant: 0)
let heightConstraint = NSLayoutConstraint(item: onView, attribute: .height, relatedBy: .equal,
toItem: onView.superview, attribute: .height, multiplier: 0.6, constant: 0)
let xConstraint = NSLayoutConstraint(item: onView, attribute: .centerX, relatedBy: .equal, toItem:onView.superview , attribute: .centerX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: onView, attribute: .centerY, relatedBy: .equal, toItem: onView.superview, attribute: .centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([widthConstraint, heightConstraint, xConstraint, yConstraint])
}
Further you can add popup and popdown Animation to your custom view.
Comment me if you need anything else.