enter image description here我在操作表中有一个收藏夹视图,以显示画廊中的一些照片

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


我希望收藏夹视图具有水平滚动视图,但是它是垂直的,并且将其设置为水平,所以我什至无法滚动!

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中使用收藏夹 View 显示照片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54006731/

10-09 12:49