iosswiftuitableviewuiimageuicontextualaction

iOS 12 UIContextualAction image is not inferring image color


in iOS 12 UIContextualAction is not inferring the colour of the picture. While it works for iOS 13 and more. I have tried all rendering modes of the icon, but still not working.

    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
}

enter image description here enter image description here


Solution

  • You can try with this

    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
    }