swift - 快速更改表格视图滑动按钮图像图标颜色
问题描述
我想实现表格视图滑动动作。有两个按钮。我想为按钮放置两个图像图标。但是图像图标颜色变为白色而不是原来的颜色。如何保持相同的图像图标颜色?我的代码是 -
//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!)
就是这样,希望对你有帮助
推荐阅读
- flutter - Flutter上如何实现Listview设计
- reactjs - 是什么导致我的 React Native App 出现这种情况:AsyncStorage 问题
- visual-studio-2019 - 如何使用 .NET Framework 4.7.2 使 CefSharp 支持 HTML5
- haskell - 了解 reader monad 如何模拟全局变量
- angular - Angular:导入模块的组件可以访问父模块中提供的服务吗?
- python - Python BMI 公式
- dictionary - kkv映射的最优数据结构
- javascript - Mocha Chai:深度包含对象数组,但只有部分预期对象
- bash - 将行的第一部分与固定字符串匹配并计算第二部分中的唯一值
- genetic-algorithm - 遗传算法适应度和交叉选择