[英]Using Collection View in ActionSheet to show photos
enter image description here I have a collection view in action sheet to show some photos from gallery 在此处输入图片描述,我在操作表中有一个收藏夹视图,以显示画廊中的一些照片
I have searched the whole StackOverflow and google the code below is the final result 我已经搜索了整个StackOverflow,并用google将下面的代码作为最终结果
var collectionView:UICollectionView!
let layout :UICollectionViewFlowLayout! = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: 70, height: 70)
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
override func viewDidLoad() {
super.viewDidLoad()
let alert:UIAlertController=UIAlertController(title: "choose
photo", message: "\n\n\n\n\n", preferredStyle: UIAlertController.Style.actionSheet)
let margin:CGFloat = 40.0
let viewmargin:CGFloat = 10.0
let rect = CGRect(x: viewmargin, y: margin, width: alert.view.bounds.size.width - viewmargin * 4.0, height: 100)
let customView = UIView(frame: rect)
// customView.backgroundColor = .green
customView.layer.masksToBounds = true
customView.layer.cornerRadius = 5
self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(UINib(nibName: "PhotoInActionSheetCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "PhotoCollectionCell")
self.collectionView.backgroundColor = UIColor.clear
customView.addSubview(self.collectionView)
self.collectionView.topAnchor.constraint(equalTo: customView.topAnchor, constant: 0).isActive = true
self.collectionView.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 0).isActive = true
self.collectionView.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: 0).isActive = true
self.collectionView.bottomAnchor.constraint(equalTo: customView.bottomAnchor, constant: 0).isActive = true
alert.view.addSubview(customView)
let cameraAction = UIAlertAction(title: "camera", style: UIAlertAction.Style.default)
{
UIAlertAction in
self.openCamera()
}
let gallaryAction = UIAlertAction(title: "gallery", style: UIAlertAction.Style.default)
{
UIAlertAction in
self.openGallary()
}
let cancelAction = UIAlertAction(title: "cancel", style: UIAlertAction.Style.cancel)
{
UIAlertAction in
}
self.imagePicker.delegate = self
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
//MARK: - colection view in actionsheet
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionCell", for: indexPath) as! PhotoInActionSheetCollectionViewCell
cell.ImagePre.image = imageArr[indexPath.row]
cell.ImagePre.layer.cornerRadius = 5
cell.ImagePre.layer.masksToBounds = true
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellSize = CGSize(width: 70, height: 70)
//
return cellSize
}
I expect to have a horizontal scroll view for collection view, but it is vertical and as I have set it to horizontal, I can't even scroll! 我希望收藏夹视图具有水平滚动视图,但是它是垂直的,并且将其设置为水平,所以我什至无法滚动!
Replace: 更换:
self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: alert.view.bounds.size.width - viewmargin * 4.0, height: 100), collectionViewLayout: layout)
with: 有:
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
With the old one!! 与旧的! And u have 2 of this , set the first one to CGRect.zero
并且您有2个,将第一个设置为CGRect.zero
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.