首页 > 解决方案 > 快速更改表格视图滑动按钮图像图标颜色

问题描述

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

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

标签: swifttableview

解决方案


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!)

就是这样,希望对你有帮助


推荐阅读