swift - 使用 ActionSheet 中的 Collection View 显示照片

标签 swift image uicollectionview uiactionsheet

enter image description here我在操作表中有一个 Collection View 来显示图库中的一些照片

我搜索了整个 StackOverflow 并谷歌了下面的代码是最终结果

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
}

我希望 Collection View 有一个水平 ScrollView ,但它是垂直的,并且由于我将其设置为水平,所以我什至无法滚动!

picture to show the result

最佳答案

替换:

self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: alert.view.bounds.size.width - viewmargin * 4.0, height: 100), collectionViewLayout: layout)

与:

collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)

和旧的一起!! 你有 2 个,将第一个设置为 CGRect.zero

关于swift - 使用 ActionSheet 中的 Collection View 显示照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006731/

相关文章:

ios - 自定义 Popover TableView

memory-management - 大图像的 UICollectionView 的内存未释放

swift - Vapor 4 与 Heroku 的预定工作

ios - 如何在 Metal for iOS 中链接过滤器?

swift - 如何首先显示另一个 View Controller (在 GameViewController.swift 之前)

iOS:UITextField 显示为 <<error type>>

html - 控制 HTML 中的图像加载顺序

image - OpenCV 在另一个图像上绘制一个图像

ios - 如何在 Collection View 的底部/页脚中添加 uiactivity 指示器?

javascript - 如何在十六进制网格上用图像替换悬停文本