0

Here is some trouble I am having working with a UITableView, more precisely using an image in tableView:editActionsForRowAt:

Below follows my relevant code. It may be useful to say that the images I am using here are all square 70x70.

func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    var patternImg:UIImage?

    let upBtn = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("upBtn button tapped")
    }
    patternImg = self.swipeCellImage(named: "UpIcn", side: 46.0)
    upBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let downBtn = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("downBtn button tapped")
    }
    patternImg = swipeCellImage(named: "DownIcn", side: 46.0)
    downBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let rmvBtn = UITableViewRowAction(style: .destructive, title: "") {
        action, index in
        print("rmvBtn button tapped")
    }
    patternImg = swipeCellImage(named: "TrashIcn", side: 46.0)
    rmvBtn.backgroundColor = UIColor(patternImage: patternImg!)

    return [downBtn,upBtn,rmvBtn]
}


func swipeCellImage(named name: String, side: CGFloat) -> UIImage? {
    let theImage = UIImage(named: name)
    UIGraphicsBeginImageContextWithOptions(CGSize(width: side*2, height: side), false, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()
    context!.setFillColor(UIColor.clear.cgColor)
    theImage?.draw(in: CGRect(x: 0, y: 0, width: side, height: side))
    let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resultImage
}

Things work, but unfortunately only up to a certain point. Looking at this screen shot shows that there is an unwanted repetion of the images. Clearly I want each button(Icon) to only appear one time each.

enter image description here

Can anyone see how I need to fix my code to avoid this repetion?

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

2

To avoid that, you have to increase width size asper your requirement.

    UIGraphicsBeginImageContextWithOptions(CGSize(width:      self.view.frame.width, height: commonHei), false, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()

    context!.setFillColor(textColor.cgColor) // CHECK BY CHANGE BGCOLOR
    context!.fill(CGRect(x: 0, y: 0, width: (self.view.frame.width) / 3, height: commonHei)) // INCREASE WIDTH SIZE
    var img: UIImage = UIImage(named: "pause")!
    img.draw(in: CGRect(x: 0, y: 0, width: 30, height: 30)) // Change position as per ur requirement.
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

If you having more doubt, then check on the 3D view.

enter image description here

McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • Yes. That works, thanks. I use: width: UIScreen.main.bounds.width. I still have no good way to set the height though; 46.0 is just something which happen to work with my current device, I cannot use rowHeight (it is -1). – Michel Feb 02 '18 at 09:12
  • U r row height Automatic? UITableViewAutomaticDimension? – McDonal_11 Feb 02 '18 at 09:16
  • I also tried UITableViewAutomaticDimension, but it did not make any difference(rowHeight is always -1). Beside following the (2) indications you put in your answer (i.e. "Increase width" and "Change position"), I indeed get a much better graphic result. But when touching, it seems the button have kept their old position, in other words the images do not match the touch areas any more. About the up vote, I thought "accepting the answer" was the best, but anyway, I will change as you prefer. – Michel Feb 02 '18 at 15:20
  • I have once more reformulated the question in its new shape here: https://stackoverflow.com/questions/48586601/using-buttons-images-in-tablevieweditactionsforrowat – Michel Feb 02 '18 at 16:10