ios - 如何以编程方式在 TableViewCell 中设置修复 ImageView 大小?
问题描述
我有这个问题,我需要修复 tableviewcell 内图像的大小。下图显示图像大小不均匀。
这是我使用的代码。
if noteImageIsAvailable == true {
if let imageData = assignedNotePhoto?.photoData {
if let image = Utilities.resizePictureImage(UIImage(data: imageData as Data)) {
//added fix
cell.imageView?.frame.size = CGSize(width: 36, height: 24)
cell.imageView?.clipsToBounds = true
//-----
cell.imageView?.contentMode = .scaleAspectFill
cell.imageView?.image = image
}
}
}
我在stackoverflow中阅读了一个答案。它说我需要添加clipToBounds
,但不幸的是它不起作用。请帮我解决这个问题。谢谢
表视图代码
extension SettingNoteViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Menu.SettingNote.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")
let keyPass = KeyHelper.NoteSetting.info[indexPath.row]
let assignedNotePhoto = self.getNotePhoto(key: keyPass.key)
let assignedNoteTextData = self.getNoteTextData(key: keyPass.key)?.value
cell.contentView.backgroundColor = .black
cell.textLabel?.textColor = .white
cell.detailTextLabel?.textColor = .white
cell.detailTextLabel?.numberOfLines = 0
let noteImageIsAvailable = assignedNotePhoto?.photoData != nil
if noteImageIsAvailable == true {
if let imageData = assignedNotePhoto?.photoData {
if let image = Utilities.resizePictureImage(UIImage(data: imageData as Data)) {
//added fix
cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
cell.imageView?.frame.size = CGSize(width: 36, height: 24)
cell.imageView?.clipsToBounds = true
//-----
cell.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
cell.imageView?.image = image
}
}
}
cell.textLabel?.text = Menu.SettingNote.items[indexPath.row].value
cell.detailTextLabel?.text = assignedNoteTextData ?? "noteSettingSubTitle".localized
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is InputMemoViewController {
let vc = segue.destination as! InputMemoViewController
vc.infoNoteKey = self.infoNoteKeyToPass
vc.infoLabelText = self.infoLabelToPass
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.infoNoteKeyToPass = KeyHelper.NoteSetting.info[indexPath.row].key
self.infoLabelToPass = KeyHelper.NoteSetting.info[indexPath.row].label
self.performSegue(withIdentifier: "showInputMemo", sender: self)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
解决方案
我有同样的问题,scaleAspectFit
为我解决了。你可以试试看
cell.imageView.contentMode = UIViewContentMode.scaleAspectFit
推荐阅读
- javascript - 如果未提供某些参数,是否阻止 Javascript 构造函数创建新实例?
- python - 如何使用 Colaboratory 复制 google 文件?
- java - 如何根据用户输入重命名分散在程序中的文件路径实例?
- ios - iOS 发布应用不再在 iPhone 上可用
- vue.js - 如何在 Vue CLI 中使用静态 JS?
- machine-learning - 为什么使用 tf.data 管道训练 tensorflow 模型会产生与直接使用 EagerTensors 提供完全不同的结果?
- java - 根据变量更改 ProgressBar 的颜色
- sql - Oracle函数将所有名称替换为其昵称?
- sql - 如何在表格中获取当前和上个月的数据?
- postgresql - PostgreSQL:“psql:错误:致命:角色“postgres”不存在”错误