gpt4 book ai didi

ios - 如何使图像适合CollectionView?

转载 作者:行者123 更新时间:2023-12-01 16:20:36 27 4
gpt4 key购买 nike

目前我有下图:
enter image description here

我想在容器中插入图片。
但是当我运行这段代码

//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
}

结果是这样的:
enter image description here

我的理解是图像大于框架,导致框架扩大。

如何在内部设置图像,使其适合第一张图像中的框?

最佳答案

欢迎使用Stackoverflow。

为此,您需要添加显式的高度宽度约束。我假设您具有返回sizeForItemAt方法的适当大小。

最后,不要忘记将cellImageViewclipsToBounds设置为true

关于ios - 如何使图像适合CollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60922411/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com