我已經在下面實作了以下方法,雖然功能有效,但顯示的影像是完全白色的。編輯樣式中是否有需要更改的屬性?
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completion in
tableView.beginUpdates()
self.viewModel.remove(atIndex: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
completion(true)
}
deleteAction.image = UIImage(named:"trash-can") // Image is red in Assets but here it's completely white
deleteAction.backgroundColor = UIColor(named: "Navy") ?? .blue
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
}

謝謝
uj5u.com熱心網友回復:
所以我設法找到了答案,這歸結為兩件事。首先,視圖層次結構在事后顯示應用于影像的白色色調,這是無法操作的。其次,為了解決該問題,需要使用 CGImage 創建影像并將其包裝在覆寫渲染模式類中。
這是作業代碼。此外,在實施時,請注意比例因子 - 在我的情況下,我需要影像為視圖寬度的一半,并且需要 1.5 的比例,而不是您自然添加的 0.5 :)
UIImage 包裝類:
class OriginalImageRender: UIImage {
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
return self
}
}
執行:
let cgImageX = UIImage(named: "trash-can")?.cgImage
deleteAction.image = OriginalImageRender(cgImage: cgImageX!, scale: 1.5, orientation: .up)
deleteAction.backgroundColor = UIColor(named: "Navy") ?? .blue
uj5u.com熱心網友回復:
UIImage(named: "trash-can").withRenderingMode(.alwaysOriginal)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368495.html
