我正在尝试更改垃圾系统图像的颜色,但由于某种原因它不起作用。
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: nil) { (ac: UIContextualAction, view: UIView, success: (Bool) -> Void) in
let cell = tableView.cellForRow(at: indexPath) as? HeroTableViewCell
if let _ = cell?.nameLabel.text {
self.deleteHeroName(index: indexPath)
}
success(true)
}
deleteAction.image = UIImage(systemName: "trash")?.withTintColor(.red)
return UISwipeActionsConfiguration(actions: [deleteAction])
}
我需要它,因为我试图为我的删除操作提供清晰的背景,并且图像颜色为白色。
对我来说,添加渲染模式为
.alwaysOriginal
(注意,而不是我们通常用于对图像着色的.alwaysTemplate
)有效:
action.image = UIImage(systemName: "trash")?.withTintColor(.red, renderingMode: .alwaysOriginal)
// Updated the image rendering to `.alwaysTemplate` and applied a tint color to ensure the icon color can be customized.
// This change was necessary because `UIContextualAction` often ignores custom tint colors unless the image is rendered as a template.
if let deleteBinImage = UIImage(systemName: "trash")?.withRenderingMode(.alwaysTemplate){
let tintedImage = deleteBinImage.withTintColor(.red, renderingMode: .alwaysOriginal)
deleteAction.image = tintedImage
}