【问题标题】:Back button on UICollectionViewUICollectionView 上的后退按钮
【发布时间】:2016-11-13 18:15:17
【问题描述】:

我正在尝试向我的 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)

【问题讨论】:

    标签: swift button uiviewcontroller back


    【解决方案1】:

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

    你有两个选择:

    1. 将集合视图控制器(和主视图控制器)放在导航控制器中,这样您就可以使用导航控制器已经实现的默认后退按钮。
    2. 您可以从主视图控制器模态显示集合视图控制器。然后,您需要添加一个关闭按钮(不是后退按钮)来关闭控制器(主视图控制器将保持在“后面”,因此当您关闭 UICollectionViewController 时,它将再次可见。

    路还很长。我建议你阅读来自 Apple 的getting started guide,在那里你可以弄清楚导航控制器是如何工作的以及它们是做什么的。这是你在使用 Swift 开发时需要学习的东西。我建议您进一步阅读整个教程。读完那一章,你应该了解 iOS 应用的导航流程,并实现后退按钮导航。

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

    【讨论】:

    • 感谢您的回复 :) 我对 swift 很陌生,所以它不是那么容易理解。我已经用完整的课程代码更新了我的问题。我想我正在使用 UIVollectionView,您对实现后退按钮功能有什么建议吗?
    • 是的,您可以使用我给您的两个选项中的任何一个。我相信两者都是相同的复杂性,所以这取决于什么更适合你。您可以寻找导航控制器的教程来开始。
    • 你能给我举个例子来说明你的意思吗?我很难理解如何实施您的建议
    • 奥斯卡如果这对你有用,你应该接受这个作为正确答案。 @Vladimir 这对我有帮助。谢谢。
    猜你喜欢
    • 2013-05-18
    • 2017-09-07
    • 1970-01-01
    • 2012-09-02
    • 2021-11-12
    • 2011-10-26
    • 2021-12-28
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多