我想.trailingSwipeActionsConfigurationForRowAt
用原始图像颜色的图像创建动作。默认情况下,所有图像都变为白色。在我的图像中,颜色很少,我想保留它们,所以我不能只着色。我正在尝试使用.withRenderingMode(.alwaysOriginal)
,但它似乎根本不起作用 - 图像仍然变白,我尝试过 pdf 和 png,都一样。
这是我的代码:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let isOn = UIContextualAction(style: .normal, title: "", handler: { _, _, completionHandler in
completionHandler(true)
})
isOn.backgroundColor = UIColor(red: 183/255, green: 189/255, blue: 196/255, alpha: 1)
isOn.image = UIImage(named: "SwitchOn")?.withRenderingMode(.alwaysOriginal)
return UISwipeActionsConfiguration(actions: [isOn])
}
有什么办法让它工作吗?
升级版:
我发现只有一种方法 - 使用.backgroundColor = UIColor(patternImage: theImage)
而不是.image
.
但是,如果您将使用这个技巧,就像我最后所做的那样 - 请注意,您将需要创建一个具有精确高度作为单元格高度的图像,并且在图像的右侧具有扩展的色域。这将有助于防止图像在屏幕区域可见重复。而且,文本标题会出现(使用.image
时是隐藏的),所以如果你不需要它,只需在其中输入一个空字符串。
代码示例:
let theImage: UIImage? = UIImage(named:"MyImage")
action.backgroundColor = UIColor(patternImage: theImage!)
图片示例: