简体   繁体   English

滚动时 CollectionViewCell 图像不适合单元格

[英]CollectionViewCell Image does not fit in the cell while scrolling

点击查看图片

Hi Everyone,嗨,大家好,

I am having a collectionView of images.我有一个图像的collectionView。 While scrolling horizontally, the images don't fit in the cell.水平滚动时,图像不适合单元格。 I am running it in the iPhone 11 pro max and the width is 414.我在 iPhone 11 pro max 上运行它,宽度是 414。

I have enabled the paging.我已启用分页。 I have tried, aspect fit, fill.我已经尝试过,方面适合,填充。 It doesn't help much.它没有多大帮助。 I want to fit the images to the cell while scrolling我想在滚动时使图像适合单元格

import UIKit

class ImageViewController:UIViewController,UICollectionViewDelegate, UICollectionViewDataSource {


    let cellIdentifier = "SecondImageCell"
    var images: [String] = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
    var indexpath = NSString()
    var startIndex = Int()

override func viewDidLoad() {


    super.viewDidLoad()
    indexpath = UserDefaults.standard.value(forKey: "indexPath") as! NSString
     self.paging_ref.numberOfPages = images.count

    }


    @IBOutlet weak var paging_ref: UIPageControl!
    @IBOutlet weak var secondCollectionView: UICollectionView!

    override func viewDidLayoutSubviews()
    {
        super.viewDidLayoutSubviews()
         let str = "\(indexpath)" // here i'm converting indexpath which is NSString into String
         startIndex = Int(str)!
        // here i'm converting str into integer value
        startIndex -= 1
        let indexPath = IndexPath(item: startIndex, section: 0)
        self.secondCollectionView.isPagingEnabled = true
        self.secondCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
        print(indexPath)
    }


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

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! SecondCollectionViewCell
     cell.layer.shouldRasterize = true
    cell.imageView2.image = UIImage(named: images[indexPath.row])
     return cell

 }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
           let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
           self.paging_ref.currentPage = Int(pageIndex)
       }

}

I think it's either your constraints are off or your content mode isn't aspectFit我认为要么是您的约束已关闭,要么您的内容模式不是aspectFit

SecondCollectionViewCell: UICollectionViewCell {
...
// 1. make sure your image is constrained fully
// 2. imageView2.contentMode = .scaleAspectFit
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CollectionViewCell在滚动时会产生随机的事物 - CollectionViewCell makes random things while scrolling Collectionview 图像在滚动时覆盖且不适合屏幕 - Collectionview images overlay while scrolling and does not fit the screen 在传递单元格数据时将Tap事件添加到CollectionViewCell - Add a tap event to CollectionViewCell while passing Cell data 选择单元格时如何自动将 collectionviewcell 滚动到最近的单元格(上一个或下一个) - How to automatically scrolling collectionviewcell to nearest cell(previous or next) when selecting a cell UITableView在快速向上滚动时不呈现单元格 - UITableView does not render cell while scrolling up quickly 从UITableView单元格中的URL加载异步图像 - 滚动时图像变为错误的图像? - Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling? 从 UITableView 单元格内的 url 加载异步图像 - 滚动时图像更改为错误图像 - Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling 如何用图像适应整个单元格 - How to fit full cell with an image UICollectionViewCell:滚动时单元格被更改 - UICollectionViewCell : Cell is changed while Scrolling 图片不适合UIImageView - Image does not fit in UIImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM