iOS 12 UIContextualAction 图像未推断图像颜色

iOS 12 UIContextualAction image is not inferring image color

in iOS 12 UIContextualAction 未推断图片的颜色。虽然它适用于 iOS 13 甚至更多。 图标的所有渲染模式都试过了,还是不行

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .normal, title: "") { _, _, complete in
    }
    let editAction = UIContextualAction(style: .normal, title: "") { _, _, complete in
    }
    
    deleteAction.image = UIImage(named: "trashcan")
    editAction.image = UIImage(named: "cellEdit")
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
    return configuration
}

你可以试试这个

class ImageWithoutRender: UIImage {
    override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
        return self
    }
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
    let deleteAction = UIContextualAction(style: .normal, title: "") { _, _, complete in
    }
    let editAction = UIContextualAction(style: .normal, title: "") { _, _, complete in
    }
    
    
    if let cgImageTrashcan =  UIImage(named: "trashcan")?.cgImage {
        deleteAction.image = ImageWithoutRender(cgImage: cgImageTrashcan, scale: UIScreen.main.nativeScale, orientation: .up)
    }
    
    if let cgImageCellEdit =  UIImage(named: "cellEdit")?.cgImage {
        editAction.image = ImageWithoutRender(cgImage: cgImageCellEdit, scale: UIScreen.main.nativeScale, orientation: .up)
    }
    
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
    return configuration
}