添加的UIViewController的.xib到UIViewController的按钮按在我的游戏

添加的UIViewController的.xib到UIViewController的按钮按在我的游戏

问题描述:

我有一个显示各个项目的的CollectionView我需要让这个当指数在按下的项目的视图控制器...添加的UIViewController的.xib到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并将其添加到主ViewController作为排序的子视图,但没有运气

我想显示不同的信息取决于选择什么样细胞,如果你能帮助我,我将不胜感激

+2

当然可以。什么是*确切*问题? – dfd

+0

即时通讯设法将视图控制器嵌入到另一个视图控制器我一直在做研究,可能已经找到了一种方法来做到这一点,所以我可能会删除这个问题 –

+0

好吧,如果你有一些具体问题,请发布到这里。从另一个VC嵌入(然后呈现)一个VC很容易。到目前为止,您的问题是不让任何人帮助您解决问题。 – dfd

据我了解,你要打开它从厦门国际银行加载您ViewControllers自定义视图中查看或不同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在技术上是正确的,但我想知道如果你能帮我做第二件事。当用户触摸任何地方,但弹出ViewController的视图控制器从父视图中删除第二只允许弹出的ViewController的UserInteraction防止错误和什么不**编辑**只是实现它,但代码是为UIView不是一个UIViewController,如果你能再次读我的问题,我需要在索引路径中的每个项目的自定义数据是否有可能这样做? –

+0

我知道这会对您有所帮助,因为我已根据您的要求专门编写和测试了此代码。你可以在Popin和Popout动画中看到这个“https://stackoverflow.com/a/46285761/715290”。 –