Использование представления коллекции в ActionSheet для отображения фотографий - PullRequest
0 голосов
/ 02 января 2019

введите описание изображения здесь У меня есть вид коллекции в листе действий, чтобы показать некоторые фотографии из галереи

Я искал весь 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
}

Я ожидаю, что у коллекции будет горизонтальная прокрутка, но она вертикальная, и, поскольку я установил горизонтальную прокрутку, я даже не могу прокрутить!

изображение, чтобы показать результат

1 Ответ

0 голосов
/ 02 января 2019

Заменить:

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

...