Stop repeating UIImage background pattern image - Swift

纵饮孤独 提交于 2020-12-13 04:52:06

问题


I'm using swipe to delete in UITableView. In swipe I need to add image. When I add image it repeats. How to stop repeating

 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]
}

Problem

What I need


回答1:


The problem, as you might have guessed, is here:

deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))

You are creating a UIColor using the (patternImage:) initializer. This means that the image will be repeated (See the word "pattern"?).

From the 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.

To solve this, you can resize your image so that it fits the frame of the button programmatically. See The simplest way to resize an UIImage?

Alternatively, use MGSwipeTableViewCell, it allows you to put images on swipe buttons.



来源:https://stackoverflow.com/questions/47709819/stop-repeating-uiimage-background-pattern-image-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!