How to scale image in UITableViewRowAction image so that it matches dynamic cell height

I'm trying to customize the UITableViewRowAction buttons, so instead of the default button, I use an image.

My problem is that the cell height is dynamic, so if the cell height was so big, the image in the UITableViewRowAction repeats! Like this:

enter image description here

so how can I keep the image always in the center, regardless of the height of the cell?

my code is:

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    //delete location 

    let deleteAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "         ", handler: {(tableViewRowAction:UITableViewRowAction, index:NSIndexPath) -> Void in


        self.deleteFavouriteLocationFromList(indexPath.row)


    })


    let image:UIImage = UIImage(named: "delete_icon_cell")!
    image.imageWithRenderingMode(UIImageRenderingMode.Automatic)

    let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(0.29, 0.3))
    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    deleteAction.backgroundColor = UIColor(patternImage: scaledImage)


    //edit location name

    let editAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "         ", handler: {(tableViewRowAction:UITableViewRowAction, index:NSIndexPath) -> Void in


        self.presentEditFavouriteLocationNamePopUp(indexPath.row)


    })


    let editImage:UIImage = UIImage(named: "edit_icon_cell")!
    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    editImage.drawInRect(CGRect(origin: CGPointZero, size: size))

    let editScaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    editAction.backgroundColor = UIColor(patternImage: editScaledImage)



    return [deleteAction,editAction]


}

thank.

+4
source share

Source: https://habr.com/ru/post/1652978/


All Articles