我想实现表格视图滑动操作。有两个按钮。我想为按钮放置两个图像图标。但是图像图标的颜色变为白色,而不是其原始颜色。如何保持相同的图像图标颜色?我的代码是-

//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
}

最佳答案

1-在课程结束时添加此课程

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


2-创建一个cgImage形式的UIImage

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


3-将此cgImage传递给类OriginalImageRender并分配给您的操作

action.image = OriginalImageRender(cgImage: cgImageX!)


就是这样,希望对您有所帮助

10-06 09:38