【问题标题】:Adding an image to UITableViewCell in edit mode isn't aligning properly在编辑模式下将图像添加到 UITableViewCell 未正确对齐
【发布时间】:2018-03-10 17:23:26
【问题描述】:
我正在使用下面的代码来尝试获得这样的外观:
但它看起来像:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete") { action, indexPath in
}
deleteRowAction.backgroundColor = UIColor.red
deleteRowAction.backgroundColor = UIColor(patternImage: UIImage(named: "DeleteIcon.png")!)
return [deleteRowAction]
}
我做错了什么?
【问题讨论】:
标签:
ios
swift
image
uitableview
edit
【解决方案1】:
在 IOS 11 以下,editActionsForRowAt 方法在背景中显示图案图像,如果图像小于单元格大小,则重复图案图像。
这就是为什么要使用第三方来实现这个SwipeCellKit
如果你在 IOS 11 中实现这个,然后使用这个 UITableViewDelegate 函数,它可以完美地工作示例:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
// Call edit action
// Reset state
success(true)
})
deleteAction.image = #imageLiteral(resourceName: "Delete")
deleteAction.backgroundColor = .red
let value = UISwipeActionsConfiguration(actions: [deleteAction])
value.performsFirstActionWithFullSwipe = true
return value
}
【解决方案2】:
只是改变:
deleteRowAction.backgroundColor = UIColor(patternImage: UIImage(named: "DeleteIcon.png")!)
到:
deleteRowAction.image = UIImage(named: "DeleteIcon.png")
它应该可以工作。