尽管图像很大,UIImageView在UICollectionViewCell中显得非常小

Abh*_*bhi 1 iphone ios swift

使用自动布局,我创建UICollectionView并添加了一个UIImageView内部来显示各种类别的图像.但是当我运行应用程序时,虽然单元格在屏幕上占据了适当的区域但是UIImageView包含在内部却显得非常小.有任何建议如何解决这个问题?我正在使用Swift.

使用自动布局的Pin选项,我已固定UIImageView到所有边缘UICollectionViewCell.

在Xcode故事板中,它看起来像这样: 单元尺寸为150 x 150

运行应用程序后,它看起来像这样: 在模拟器中的应用程序视图

我想UIImageView覆盖细胞的所有绿色空间来显示图像.我正在使用以下代码填充UICollectionView

@IBOutlet weak var categoryCollectionView: UICollectionView!

let categoryImages = [UIImage(named: "cake"), UIImage(named: "flowers"), UIImage(named: "chocolate"), UIImage(named: "greetings"), UIImage(named: "combobox"), UIImage(named: "handicraft"), UIImage(named: "mug"), UIImage(named: "soft_toy")]

let categoryNames = ["Cakes", "Flowers", "Chocolates", "Greetings", "Combos", "Handicraft", "Mugs", "Soft Toys"]


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.categoryNames.count;
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = categoryCollectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! HomeCategoriesCollectionViewCell


    cell.categoryImageView?.image = self.categoryImages[indexPath.row]

    cell.categoryNameLabel?.text = self.categoryNames[indexPath.row]

    return cell
}
Run Code Online (Sandbox Code Playgroud)

Ror*_*nel 6

看起来contentView没有填充代码返回的单元格宽度/高度.contentView在创建单元格后,您可以尝试设置框架以匹配单元格的边界,如下所示cellForItemAtIndexPath:

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | 
                         UIViewAutoresizing.FlexibleWidth | 
                         UIViewAutoresizing.FlexibleRightMargin |
                         UIViewAutoresizing.FlexibleTopMargin |
                         UIViewAutoresizing.FlexibleHeight | 
                         UIViewAutoresizing.FlexibleBottomMargin
Run Code Online (Sandbox Code Playgroud)

或斯威夫特2

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleLeftMargin,
                          .FlexibleWidth,
                          .FlexibleRightMargin,
                          .FlexibleTopMargin,
                          .FlexibleHeight,
                          .FlexibleBottomMargin]
Run Code Online (Sandbox Code Playgroud)