使用自动布局,我创建了UICollectionView
并在其中添加了一个UIImageView
以显示各种类别的图像。但是当我运行这个应用程序时,尽管这个单元格正在屏幕上显示适当的区域,但其中包含的UIImageView
显得非常小。任何建议如何解决这个问题?我正在使用Swift。UIImageView在UICollectionViewCell中显示得非常小,虽然图像很大
使用自动布局的引脚选项,我已将UIImageView
固定到UICollectionViewCell
的所有边缘。
我想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
}
确保您添加的任何约束/任何尺寸的课程。调试日志中是否存在任何约束错误?你可以尝试设置UIImageView的背景颜色来查看它相对于图像的大小,以便知道它的图像约束或图像渲染是否错误。 –
我已经通过应用背景到UIImageView进行检查,但它的尺寸也与它内部的图像相同。不,在调试日志中没有错误。我已经添加约束使用自动布局中的'Pin'选项来UIImageView – Abhi
我不知道swift,但只是尝试在cellForItemAtIndexPath方法数据源方法中设置imageview的帧(cell.imageView.frame = CGRectMake(0,0,cell .frame.size.width,cell.frame.size.height);正如我们在Objective-c中所做的那样),我认为它会起作用。 – abhi1992