首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ActionSheet中使用集合视图显示照片

在ActionSheet中使用集合视图显示照片
EN

Stack Overflow用户
提问于 2019-01-02 20:52:14
回答 1查看 567关注 0票数 0

enter image description here我在操作表中有一个集合视图,用于显示图库中的一些照片

我已经搜索了整个StackOverflow和谷歌,下面的代码是最终结果

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-02 21:30:03

替换:

代码语言:javascript
复制
self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: alert.view.bounds.size.width - viewmargin * 4.0, height: 100), collectionViewLayout: layout)

通过以下方式:

代码语言:javascript
复制
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)

用旧的!!你有2个,把第一个设为CGRect.zero

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54006731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档