首页 > 解决方案 > Swift:ScrollView 根据单元格图像大小滚动

问题描述

我有一个viewControllerwhich hasCollectionView并且 collectionView 单元格包含ImageView并且响应来自api. 而且viewController orientationLandscape。所以问题是当我滚动图像时它没有正确滚动。这是附加图像。

图 1 图 2

那么如何根据Scrolling action我的图像大小进行设置。所以它在视图中只显示图像。这是我的代码。

extension GalleryViewController : UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
            let image = arrImgs[indexPath.row]
            cell.lblIndex.text = "< \(indexPath.row+1) / \(arrImgs.count) >  Images"
            if let imgUrl = URL.init(string: image.Message) {
                cell.imgV.sd_setImage(with: imgUrl) { (img, error, casheType, url) in
                    DispatchQueue.main.async {
                        cell.imgV.image = img
                    }
                }
            }
            return cell
        }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return arrImgs.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize.init(width: width, height: height)
    }
}

class ImageCell: UICollectionViewCell {

    @IBOutlet var lblIndex: UILabel!
    @IBOutlet var imgV: UIImageView!

}

这是我的StoryBord安排。

图 3

谢谢。

标签: iosswiftuiscrollviewuicollectionview

解决方案


推荐阅读