Application Developer
Application Developer

Reputation: 105

To change tableview swipe button image icon color in swift

I want to implement table view swipe action. Two buttons are there. I want to place two image icon for the buttons.But the image icon color changed to white instead of its original color.How to keep the same image icon color ? My code is -

//MARK: - Tableview delegate

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}


// Trailing swipe action for tableview cell

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let action =  UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler ) in
        //do stuff
        completionHandler(true)
    })

    // create action for the delete button
    action.image = UIImage(named: "DeleteChat.png")?.scaleImageToSize(newSize: CGSize(width: 40, height: 40)).withRenderingMode(.alwaysOriginal)
    action.backgroundColor = .red


    //create action for the block button
    let block =  UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler ) in
        //do stuff
        completionHandler(true)
    })
    block.image = UIImage(named: "BlockUser.png")?.scaleImageToSize(newSize: CGSize(width: 40, height: 40)).withRenderingMode(.alwaysOriginal)
    block.backgroundColor = .black

    // Add both the delete button and add button here
    let confrigation = UISwipeActionsConfiguration(actions: [ action, block])

    return confrigation
}

Upvotes: 2

Views: 1160

Answers (1)

Awais Mobeen
Awais Mobeen

Reputation: 852

1- add this class at the end of your class

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

2- create a cgImage form UIImage

 let cgImageX =  UIImage(named: "delete")?.cgImage

3- pass this cgImage to class OriginalImageRender and assign to your action

action.image = OriginalImageRender(cgImage: cgImageX!)

that's it, hope helpful to you

Upvotes: 1

Related Questions