使用 Collection 在 ActionSheet 中查看以显示照片

Using Collection View in ActionSheet to show photos

enter image description here我有一个 collection 视图正在运行 sheet 以显示图库中的一些照片

我搜索了整个 Whosebug 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
}

我希望 collection 视图有水平滚动视图,但它是垂直的,因为我已将其设置为水平,所以我什至无法滚动!

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