我想在容器中插入图片。但是当我运行这段代码时
//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
}
我的理解是图像大于导致框架扩展的框架。
如何在内部设置图像,使其适合第一张图像中的框?
这是一个更简单的问题。因此,将来我建议您进行一些搜索,因为那样您会更快地学习基础知识。
cell.ImageView.image = UIImage.init(named: "page-01")
cell.ImageView.contentMode = .scaleAspectFill
cell.clipsToBounds = true
这是基本清单。当您访问图像视图上的.contentMode属性时,您可能想尝试可用的选项,以查看最喜欢的选项。
https://developer.apple.com/documentation/uikit/uiview/1622619-contentmode
欢迎使用Stackoverflow。
为此,您需要添加显式的height和width约束。我假设您有适当的大小可以在方法sizeForItemAt
中返回。
最后,不要忘记将cell
或ImageView
的clipsToBounds
设置为true
。