停止重复 UIImage 背景图案图像 - Swift
Stop repeating UIImage background pattern image - Swift
我在 UITableView 中使用滑动删除。在滑动中我需要添加图像。当我添加图像时,它会重复。如何停止重复
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
return
}
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
deleteButton.title = ""
return [deleteButton]
}
问题
我需要什么
您可能已经猜到了,问题出在这里:
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
您正在使用 (patternImage:)
初始值设定项创建 UIColor
。这意味着图像将被重复(看到这个词"pattern"?)。
来自docs:
You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.
要解决此问题,您可以调整图像大小,使其以编程方式适合按钮的框架。参见 The simplest way to resize an UIImage?
或者,使用 MGSwipeTableViewCell,它允许您将图像放在滑动按钮上。
我在 UITableView 中使用滑动删除。在滑动中我需要添加图像。当我添加图像时,它会重复。如何停止重复
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
return
}
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
deleteButton.title = ""
return [deleteButton]
}
问题
我需要什么
您可能已经猜到了,问题出在这里:
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
您正在使用 (patternImage:)
初始值设定项创建 UIColor
。这意味着图像将被重复(看到这个词"pattern"?)。
来自docs:
You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.
要解决此问题,您可以调整图像大小,使其以编程方式适合按钮的框架。参见 The simplest way to resize an UIImage?
或者,使用 MGSwipeTableViewCell,它允许您将图像放在滑动按钮上。