我正在尝试向我的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),如果可能的话
答案 0 :(得分:2)
我假设你在谈论UICollectionViewController
而不是UICollectionView
。 UICollectionViewController
内部有UICollectionView
。你可以"关闭" (解雇)UICollectionViewController
但不是UICollectionView
。您甚至可以解雇内部有UIViewController
的{{1}}。
您有两种选择:
UICollectionView
它将再次可见时。它还有很长的路要走。我建议你从Apple那里读到这个getting started guide,在那里你可以弄清楚导航控制器和它们的作用。这是使用Swift进行开发时需要学习的内容。我建议你进一步阅读整个教程。阅读完该章后,您应该了解iOS应用程序的导航流程并实现后退按钮导航。
如果您在该教程后发现任何问题,请与我们联系。