How to add image in UITableViewRowAction?

前端 未结 6 986
囚心锁ツ
囚心锁ツ 2020-11-30 05:58

I\'m trying to add image in UITableView Swipe style. I tried with Emoji text & its working fine

func tableView(_ tableView: UITableView, edi         


        
6条回答
  •  自闭症患者
    2020-11-30 06:37

    I had the same problem with my project, so I did a workaround for this. I think, this is helpful for you.

    When I swipe table cell to the left only for image width, it is working fine.

    But when I swipe table cell more than image width, table cell display like this:

    This happen because to add image I use 'backgroundColor' property.

    copyButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaCopyIcon.png")!)
    

    So to fix this, I increase image width to the same as table width.

    old image >>>>>>>>>>>> new image

    >>>>

    this is the new look:

    This is my sample code:

    func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
        let copyButton = UITableViewRowAction(style: .normal, title: "") { action, index in
    
             print("copy button tapped")
    
        }
        copyButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaCopyIcon.png")!)
    
    
        let accessButton = UITableViewRowAction(style: .normal, title: "") { action, index in
    
           print("Access button tapped")
    
        }
        accessButton.backgroundColor = UIColor(patternImage: UIImage(named: "bfaAccess.png")!)
    
    
        return [accessButton, copyButton]
    }
    

提交回复
热议问题