在 UITableViewCell 中设置图像时约束不起作用
Constraints don't work when setting an UIImage inside a TableViewCell
当我在 cellForRowAt
函数中设置 UIImage
时,看起来我设置的 UIImage
与 UIImageView
.
重叠
已编辑:clipsToBound
已标记并且 contentMode
在 Storyboard
中设置为 .scaleAspectFit
我像往常一样在 Storyboard
中设置所有 Constraints
:
但是当我 运行 应用程序时,它看起来具有更高的优先级,压倒了我设置的 Constraints
,这就是我得到的:
不久前我在其他项目中做了类似的事情,对我来说效果很好:
这是设置单元格的代码,但我很确定我只是不小心标记或取消标记了 Storyboard
:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
cell.showLoader()
cell.songNameLabel.text = songArray[indexPath.row].title
if songImageArray[songArray[indexPath.row].title] != nil {
cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
cell.hideLoader()
}
cell.setupImageDesign()
return cell
}
如果需要任何其他信息,请告诉我,提前谢谢。
将图像视图clipsToBounds
设置为true
。
更改 Storyboard 中 UIImageView 的 contentMode
并设置 clipsToBounds = true
.
将 ImageView 的 ContentMode 更改为 .scalToFill
并将剪辑 属性 更改为 clipToBounds
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
cell.showLoader()
cell.songNameLabel.text = songArray[indexPath.row].title
if songImageArray[songArray[indexPath.row].title] != nil {
cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
cell.hideLoader()
}
cell.setupImageDesign()
return cell
}
cell.imageView?.image
是来自 UITableViewCell 的默认 imageView。
您必须重命名您的自定义 UIImageView outlet
您对限制边距
有疑问
请删除所有约束,并确保在设置约束之前取消选中此选项。
在情节提要中看起来像这样
当我在 cellForRowAt
函数中设置 UIImage
时,看起来我设置的 UIImage
与 UIImageView
.
已编辑:clipsToBound
已标记并且 contentMode
在 Storyboard
.scaleAspectFit
我像往常一样在 Storyboard
中设置所有 Constraints
:
但是当我 运行 应用程序时,它看起来具有更高的优先级,压倒了我设置的 Constraints
,这就是我得到的:
不久前我在其他项目中做了类似的事情,对我来说效果很好:
这是设置单元格的代码,但我很确定我只是不小心标记或取消标记了 Storyboard
:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
cell.showLoader()
cell.songNameLabel.text = songArray[indexPath.row].title
if songImageArray[songArray[indexPath.row].title] != nil {
cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
cell.hideLoader()
}
cell.setupImageDesign()
return cell
}
如果需要任何其他信息,请告诉我,提前谢谢。
将图像视图clipsToBounds
设置为true
。
更改 Storyboard 中 UIImageView 的 contentMode
并设置 clipsToBounds = true
.
将 ImageView 的 ContentMode 更改为 .scalToFill
并将剪辑 属性 更改为 clipToBounds
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
cell.showLoader()
cell.songNameLabel.text = songArray[indexPath.row].title
if songImageArray[songArray[indexPath.row].title] != nil {
cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
cell.hideLoader()
}
cell.setupImageDesign()
return cell
}
cell.imageView?.image
是来自 UITableViewCell 的默认 imageView。
您必须重命名您的自定义 UIImageView outlet
您对限制边距
有疑问请删除所有约束,并确保在设置约束之前取消选中此选项。
在情节提要中看起来像这样