有几个类似的问题(例如my previous iOS10 variant),但我认为iOS 11的最新答案应该是使用Swift4的,它不使用私有API,也不依赖于将图标限制在unicode表情符号上。
随着API向iOS11的发展,我们现在可以将图像和文本放到模板模式中,但是它们被强制进入模板模式,并使用您设置的任何backgroundColor进行反向着色。例如。
let rename = UIContextualAction(style: .normal, title: "Rename") { (_, view, _) in
self.renameEntry(indexPath)
}
rename.backgroundColor = UIColor.black
rename.image = UIImage(named: "pencilEdit")
let locate = UIContextualAction(style: .normal, title: "Locate") { (_, view, _) in
self.locateEntry(indexPath)
}
locate.backgroundColor = UIColor.blue
locate.image = UIImage(named: "locatePin")
let delete = UIContextualAction(style: .destructive, title: "Forget") { (_, view, _) in
self.deleteEntry(indexPath)
}
delete.backgroundColor = UIColor.red
delete.image = UIImage(named: "triggerDeleteSelector")
return UISwipeActionsConfiguration(actions: [rename, locate, delete])它产生了这样的结果:

所以很明显,我们可以有图像或文本,但不能两者兼有。
但我想要的是

除了倒序之外,我不知道如何像对iOS10那样欺骗系统。我仍然可以生成文本和图像的图像,但我不能控制该图像的颜色。将backgroundColor设置为零或.clear只会产生空方块。
发布于 2017-11-20 22:14:18
使用patternImage设置背景色,不要设置标题或图像。这是一个讨厌的伎俩,但它应该适用于你的情况。
let rename = UIContextualAction(style: .normal, title: nil) { (_, view, _) in
self.renameEntry(indexPath)
}
rename.backgroundColor = UIColor(patternImage: UIImage(named: "pencilEditWideFrame")!)您必须保存像这样的宽帧图像,图标位于最左边,这样它们就不会重复:

注意:我对此进行了测试,当使用两个或多个上下文操作时,它可以工作,但只对一个操作不起作用。该模式最终在一个完整的滑动重复。
这还假设您的表视图后面有一个简单的背景,您可以使用它作为图像的背景色,因为透明度仍然是不允许的。
https://stackoverflow.com/questions/47320094
复制相似问题