1

in my game i have a ViewController that displays various items in a collectionView i need to make it so that when an item is pressed at an index...

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    if indexPath.item == 0 {
        //add a viewcontroller for viewing content
        //other things here for customizing that view data
    }
}

...a viewcontroller pops onscreen not taking up the whole screen but a small portion in the middle of the main ViewController

(i need the view to be reusable and adaptable) i have tried making a viewController and adding it to the main ViewController as a subview of sorts but no luck

i want to display different info depending what cell is selected if you could help me i would appreciate it

E. Huckabee
  • 1,788
  • 1
  • 13
  • 29
  • 2
    Sure you can. What's the *exact* issue? –  Sep 18 '17 at 14:13
  • im trying to embed a viewcontroller into another viewcontroller i have been doing research and may have found a way to do it so i might delete this question – E. Huckabee Sep 18 '17 at 15:03
  • Well, if you have something of a specific issue, post it here. Embedding (and then presenting) a VC from another VC is pretty easy. So far though, your question isn't letting anyone help you on what the issue is. –  Sep 18 '17 at 16:00

1 Answers1

1

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.

Sudhir kumar
  • 659
  • 5
  • 21
  • thank you very much as this is exactly what i needed i will begin implementing it ASAP also if you could change your anwser to include the popup and popdown animation that would be very helpful because the way i have it is very robotic and not very fluid – E. Huckabee Sep 19 '17 at 07:49
  • your anwser is technically correct but i was wondering if you could help me do two other things 1st. when the user touches anywhere but the popped up ViewController the view controller gets removed from the parent view 2nd only allow UserInteraction on the popped up ViewController to prevent bugs and what not **Edit** just implemented it but the code is designed for a UIView not a UIViewController and if you could read my question again i need customized data for each item in the index path is it possible to do that? – E. Huckabee Sep 19 '17 at 07:55
  • I know that this will help you a lot, as I have written and tested this code specially as per your requirement . You can see this "https://stackoverflow.com/a/46285761/715290" for Popin and Popout animation. – Sudhir kumar Sep 19 '17 at 07:59
  • it is designed for a UIView and i need UIViewController because the viewController will essentially function as a button btw thanks for all your help – E. Huckabee Sep 19 '17 at 08:01
  • You can use the same code for UIViewController , Create a optional property to store your viewController reference so that it still in memory once initialise . Add Constraint on ViewController view in similar fashion as describe in code above – Sudhir kumar Sep 19 '17 at 08:04
  • could you show me how to change the code by editing your anwser i will mark this as correct no matter what but could you please change your anwser to be the correct one or point me toward a question that anwsers it? – E. Huckabee Sep 19 '17 at 08:27