2017-09-18 3 views
1

をのUIViewControllerの.xibを追加...私はアイテムがインデックスに押されたときにように私はそれをする必要がありcollectionViewに様々なアイテムを表示するのViewControllerを持っているボタンを押す上のUIViewControllerに私のゲームで

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 
    } 
} 

... ViewControllerは画面全体を占めず、メインの中央に小さな部分を占めます。ViewControllerを作成して追加しようとしました。メインのViewControllerは一種のサブビューとして運行はありません。

私はあなたが私を助けることができる場合に選択されたセルに応じて別の情報を表示したい

+2

もちろん可能です。正確*問題は何ですか? – dfd

+0

他のviewcontrollerにviewcontrollerを埋め込もうとしている私は研究をしていて、それを行う方法を見つけたかもしれないので、この質問を削除するかもしれません。 –

+0

特定の問題がある場合は、ここに投稿してください。別のVCからVCを埋め込む(そして提示する)のは簡単です。これまでのところ、あなたの質問は、問題が何であるかについて誰かにあなたを助けさせていません。 – dfd

答えて

1

私の知る限り、ViewControllerビューのxibからロードされたカスタムビューを開きたいあなたのViewControllerのViewControllersビュー(collectionViewのさまざまなアイテムを表示します)。はい、コード

//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]) 

} 

の下に使用している場合

さらに、あなたのカスタムビューにポップアップやポップダウンアニメーションを追加することができます。

他に必要な場合はコメントしてください。

+0

これはまさに私が必要としてくれたことに非常に感謝しています。私はそれが非常にロボットであり、あまり流動的ではないので、非常に有用なポップアップとポップダウンのアニメーションを含むようにanwserを変更できる場合、 –

+0

あなたのanwserは技術的には正しいですが、私はあなたが私が他の2つのことを手助けできるかどうか疑問に思っていました。ユーザーが触れたときにViewControllerがポップアップされたときView Controllerは親ビューから削除されます2ndポップアップされたViewControllerのUserInteractionのみがバグを防止するために** Edit **は実装されましたがコードはUIView UIViewControllerではなく、私の質問をもう一度読むことができたら、インデックスパスの各項目のカスタマイズされたデータが必要です。 –

+0

私はあなたの要求に応じてこのコードを特別に書いてテストしたので、これがあなたを大いに助けることを知っています。この "https://stackoverflow.com/a/46285761/715290"は、ポピンとポップアウトのアニメーションです。 –

関連する問題

 関連する問題