使用自动布局,我创建了UICollectionView
并在其中添加了UIImageView
以显示各种类别图像。但是当我运行应用程序时,尽管单元格在屏幕上占据了适当的区域,但其中包含的UIImageView
显得非常小。有任何建议如何解决这个问题?我正在使用Swift。
使用自动布局的Pin选项,我已将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
}
答案 0 :(得分:6)
看起来contentView
没有填充代码返回的单元格宽度/高度。在contentView
中创建单元格后,您可以尝试设置cellForItemAtIndexPath
框架以匹配单元格的边界,如下所示:
cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin |
UIViewAutoresizing.FlexibleWidth |
UIViewAutoresizing.FlexibleRightMargin |
UIViewAutoresizing.FlexibleTopMargin |
UIViewAutoresizing.FlexibleHeight |
UIViewAutoresizing.FlexibleBottomMargin
或Swift 2
cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleLeftMargin,
.FlexibleWidth,
.FlexibleRightMargin,
.FlexibleTopMargin,
.FlexibleHeight,
.FlexibleBottomMargin]