Swift UICollectionView上的后退按钮

Swift UICollectionView上的后退按钮,swift,button,uiviewcontroller,back,Swift,Button,Uiviewcontroller,Back,我正在尝试将后退/返回按钮添加到UICollectionView,到目前为止,我有以下代码来实现该按钮: import UIKit class EmojiPopup: UIView,UICollectionViewDataSource,UICollectionViewDelegate { var collocationView : UICollectionView! var arrImagesList:NSMutableArray! var blur:UIBlurEf

我正在尝试将后退/返回按钮添加到UICollectionView,到目前为止,我有以下代码来实现该按钮:

import UIKit

class EmojiPopup: UIView,UICollectionViewDataSource,UICollectionViewDelegate
{
    var collocationView : UICollectionView!
    var arrImagesList:NSMutableArray!

    var blur:UIBlurEffect = UIBlurEffect()

    override init(frame: CGRect)
    {

        super.init(frame: frame)

        arrImagesList = NSMutableArray()
        self.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.2)
        let layout = UICollectionViewFlowLayout()
        //header gap
        layout.headerReferenceSize = CGSizeMake(20,20)
        //collection view item size
        layout.itemSize = CGSizeMake(70, 70)
        layout.minimumInteritemSpacing = 25
        layout.minimumLineSpacing = 25
        collocationView = UICollectionView(frame: CGRectMake(50,50,UIScreen.mainScreen().bounds.screenWidth - 100,UIScreen.mainScreen().bounds.screenHeight - 100), collectionViewLayout: layout)
        self.addSubview(collocationView)

        // Create the blurEffect and apply to view
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ExtraLight)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.alpha = 0.7
        blurEffectView.frame = self.bounds
        self.addSubview(blurEffectView)

        collocationView.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.002)
        collocationView.dataSource = self
        collocationView.delegate = self
        collocationView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")

        //hide scrollbar
        self.collocationView.showsVerticalScrollIndicator = false

        //back button
        let btnBack = UIButton(frame:TCRectMake(x:138 ,y:523,width:45,height:45))
        btnBack.setImage(UIImage(named:"back"), forState: UIControlState.Normal)
        btnBack.addTarget(self, action:"btnBackClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btnBack)

        //back button func
        func btnBackClick()
        {

        }

        let fm = NSFileManager.defaultManager()
        let path = NSBundle.mainBundle().resourcePath!
        let items = try! fm.contentsOfDirectoryAtPath(path)

        for item in items
        {
            if item.hasSuffix("png") && item.containsString("@") == false && item.containsString("AppIcon") == false && item.containsString("tick_blue") == false && item.containsString("video_camera") == false
            {
                arrImagesList.addObject(item)
            }
        }
    }
    var completeHandler:((String)->())?
    func showDetails(viewParent:UIView,doneButtonClick:((String)->())?)
    {
        completeHandler = doneButtonClick
        viewParent.addSubview(self)
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return arrImagesList.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let identifier="ImageCell\(indexPath.section)\(indexPath.row)"
        collectionView.registerClass(ImageViewCell.self, forCellWithReuseIdentifier:identifier)

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ImageViewCell
        cell.backgroundColor = UIColor(white:1, alpha:0)
        cell.imgView.image = UIImage(named:arrImagesList[indexPath.row] as! String)
        cell.imgView.backgroundColor = UIColor.clearColor()
        cell.imgView.opaque = false
        cell.imgView.contentMode = .ScaleAspectFit

        //keeps blur to background
        self.bringSubviewToFront(collocationView)

        return cell
    }  
//    func collectionView(collectionView: UICollectionView,
//        layout collectionViewLayout: UICollectionViewLayout,
//        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
//    {
//        let width=UIScreen.mainScreen().bounds.size.width-50
//        return CGSize(width:width/3, height:width/3)
//    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
        //let cell=collectionView.cellForItemAtIndexPath(indexPath) as! ImageViewCell

        UIView.animateWithDuration(0.3, animations:{
            self.collocationView.alpha=0
            }, completion: { finished in

                if self.completeHandler != nil
                {
                    self.completeHandler!(self.arrImagesList[indexPath.row] as! String)
                }
                self.removeFromSuperview()
        })

    }
    func showDetails(viewParent:UIView,dictData : [String:String],index:Int,doneButtonClick:(()->())?,cancelBUttonClick:(()->())?)
    {
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

如果用户按下后退按钮,我希望集合视图关闭,但我不确定在后退按钮功能中输入什么。如果可能的话,我希望用户返回主视图控制器(mapview)

我假设您谈论的是
UICollectionViewController
,而不是
UICollectionView
UICollectionViewController
内部有一个
UICollectionView
。您可以“关闭”(关闭)一个
UICollectionViewController
,但不能关闭
UICollectionView
。您甚至可以关闭内部有
UICollectionView
UIViewController

您有两个选择:

  • 将集合视图控制器(和主视图控制器)放入导航控制器中,以便可以使用导航控制器已实现的默认后退按钮
  • 可以从主视图控制器以模式显示集合视图控制器。然后,您需要添加关闭按钮(而不是后退按钮)以关闭控制器(主视图控制器将保持“落后”,因此当您关闭
    UICollectionViewController
    时,它将再次可见
  • 还有很长的路要走。我建议你从苹果公司读这篇文章,在那里你可以了解导航控制器是如何工作的。这是你在使用Swift开发时需要学习的。我建议你进一步阅读整个教程。读完这一章后,你应该了解iOS应用程序的导航流程,并实施单击后退按钮导航


    如果您在学习该教程后发现任何问题,请告诉我。

    谢谢您的回复:)我对swift很陌生,所以它不容易理解。我已经用完整的课堂代码更新了我的问题。我想我使用的是UIVollectionView,你对实现后退按钮功能有什么建议吗?是的,你可以使用我给你的两个选项中的任何一个。我相信两者的复杂性是一样的,所以这取决于什么更适合你。你可以从导航控制器的教程开始。你能给我举个例子说明你的意思吗?我发现很难理解如何实施你的建议。如果这对你有效,你应该接受这一正确答案@弗拉基米尔:这对我有帮助。谢谢。更新链接