//MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
//make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! AppIconViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.item]
cell.ImageView.image = UIImage.init(named: "page-01")
//cell.backgroundColor = UIColor.cyan
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
return cell
}
结果是这样的:
我的理解是图像大于框架,导致框架扩大。
如何在内部设置图像,使其适合第一张图像中的框?
最佳答案
欢迎使用Stackoverflow。
为此,您需要添加显式的高度和宽度约束。我假设您具有返回sizeForItemAt
方法的适当大小。
最后,不要忘记将cell
或ImageView
的clipsToBounds
设置为true
。
关于ios - 如何使图像适合CollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60922411/