2015-10-14 89 views
1

使用自动布局,我创建了UICollectionView并在其中添加了一个UIImageView以显示各种类别的图像。但是当我运行这个应用程序时,尽管这个单元格正在屏幕上显示适当的区域,但其中包含的UIImageView显得非常小。任何建议如何解决这个问题?我正在使用Swift。UIImageView在UICollectionViewCell中显示得非常小,虽然图像很大

使用自动布局的引脚选项,我已将UIImageView固定到UICollectionViewCell的所有边缘。

在Xcode的故事情节,它看起来是这样的: Cell Size is 150 x 150

运行的应用程序后,它看起来像这样: App view in simulator

我想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 
} 
+0

确保您添加的任何约束/任何尺寸的课程。调试日志中是否存在任何约束错误?你可以尝试设置UIImageView的背景颜色来查看它相对于图像的大小,以便知道它的图像约束或图像渲染是否错误。 –

+0

我已经通过应用背景到UIImageView进行检查,但它的尺寸也与它内部的图像相同。不,在调试日志中没有错误。我已经添加约束使用自动布局中的'Pin'选项来UIImageView – Abhi

+0

我不知道swift,但只是尝试在cellForItemAtIndexPath方法数据源方法中设置imageview的帧(cell.imageView.frame = CGRectMake(0,0,cell .frame.size.width,cell.frame.size.height);正如我们在Objective-c中所做的那样),我认为它会起作用。 – abhi1992

回答

6

貌似contentView不填充单元格的宽度/高度的代码返回。你可以尝试设置contentView框架相匹配的单元格的范围如下一旦你创造你的细胞在cellForItemAtIndexPath

cell.contentView.frame = cell.bounds 
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | 
         UIViewAutoresizing.FlexibleWidth | 
         UIViewAutoresizing.FlexibleRightMargin | 
         UIViewAutoresizing.FlexibleTopMargin | 
         UIViewAutoresizing.FlexibleHeight | 
         UIViewAutoresizing.FlexibleBottomMargin 

或斯威夫特2

cell.contentView.frame = cell.bounds 
cell.contentView.autoresizingMask = [.FlexibleLeftMargin, 
          .FlexibleWidth, 
          .FlexibleRightMargin, 
          .FlexibleTopMargin, 
          .FlexibleHeight, 
          .FlexibleBottomMargin] 
+0

有了这段代码,我得到错误'Binary Operator'|'不能应用于两个UIViewAutoresizing操作数' – Abhi

+0

@Abhi更新,因为它是不同的迅速2.还有一个错字,并设置框架,而不是自动调整面具。 –

+0

非常感谢:) – Abhi

相关问题