在尾随滑动操作中更改系统图像的色调颜色

问题描述 投票:0回答:2

我正在尝试更改垃圾系统图像的颜色,但由于某种原因它不起作用。

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])
}

我需要它,因为我试图为我的删除操作提供清晰的背景,并且图像颜色为白色。

swift uitableview uikit uiimage uiswipeactionsconfiguration
2个回答
2
投票

对我来说,添加渲染模式为

.alwaysOriginal
注意,而不是我们通常用于对图像着色的
.alwaysTemplate
)有效:

action.image = UIImage(systemName: "trash")?.withTintColor(.red, renderingMode: .alwaysOriginal)

0
投票
    // 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
    }
© www.soinside.com 2019 - 2024. All rights reserved.