2

I have set an action for "add to cart" at leading side of the UItableViewCell. I have set background colour, its image and title. below's my code.

 @available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let addToCart = UIContextualAction(style: .normal, title: "Add to Cart") { (action, view, nil) in
        print("Added to cart")

    }
    addToCart.title = "Add to Cart"
    addToCart.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
    addToCart.image = UIImage(named: "cart")

    return UISwipeActionsConfiguration(actions: [addToCart])
}

previous attempts : I have not defined title using addToCart.title = "Add to Cart" but, after not getting it, I have set that.

The image I have added is of 25*25 of size, has clear background and is of .png format.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Parth Rudakia
  • 151
  • 16

3 Answers3

9

UIContextualAction supports either text or image. By setting the image with setImage: property, basically remove the title when creating the object. If you want both text and image, you need to create images with embedded text.

its the bug in UIContextualAction. It does not show both image and title at the same time unless the table view cell height is 91 points. for more info you get forums.apple

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

For anyone still looking for a solution, I found this one on Medium: https://forzfilm.medium.com/how-to-set-image-and-label-in-trailing-swipe-action-with-custom-font-ios11-4a49d4794669. It turns out that you can render a UIView as an Image.

Note: the answer below is with UICollectionLayoutListConfiguration, but it you can also use it with tableView since they both use UIContextualAction. And the cell height in the example is 80.

Tested in XCode 14 with UICollectionLayoutListConfiguration:

SwipeLayout:

private func swipeLayout(icon: String, text: String, size: CGFloat) -> UIImage {
        let config = UIImage.SymbolConfiguration(pointSize: size, weight: .regular, scale: .large)
        let img = UIImage(systemName: icon, withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal)

        let label = UILabel(frame: .zero)
        label.font = .systemFont(ofSize: 13, weight: .medium)
        label.textColor = .white
        label.text = text

        let tempView = UIStackView(frame: .init(x: 0, y: 0, width: 50, height: 50))
        let imageView = UIImageView(frame: .init(x: 0, y: 0, width: img!.size.width, height: img!.size.height))
        imageView.contentMode = .scaleAspectFit
        tempView.axis = .vertical
        tempView.alignment = .center
        tempView.spacing = 2
        imageView.image = img
        tempView.addArrangedSubview(imageView)
        tempView.addArrangedSubview(label)

        let renderer = UIGraphicsImageRenderer(bounds: tempView.bounds)
        let image = renderer.image { rendererContext in
            tempView.layer.render(in: rendererContext.cgContext)
        }
        return image
    }

in trailingSwipeActionsConfigurationProvider:

// archive
let archiveHandler: UIContextualAction.Handler = { [weak self] action, view, completion in
     completion(true)
//   self?.updateSnapShot(animated: true)
}
let archiveAction = UIContextualAction(style: .normal, title: nil, handler: archiveHandler)
archiveAction.image = self.swipeLayout(icon: "archivebox.fill", text: "Archive", size: 16)
archiveAction.backgroundColor = .theme.arhiveRow

enter image description here

Thel
  • 396
  • 2
  • 8
-1

Try this

Add swipe to delete UITableViewCell

@available(iOS 11.0, *)    
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let action =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
                //do stuff
                completionHandler(true)
                let data:NSDictionary = self.conversations[indexPath.row] as! NSDictionary
                print(data)
                let alert:UIAlertController = UIAlertController(title: "", message: "are you sure want to delete ?", preferredStyle: .alert)

                alert.addAction(UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: { (action) in
                }))
                self.present(alert, animated: true, completion: nil)
            })
            action.image = UIImage(named: "")
            action.backgroundColor = UIColor(red: 0/255, green: 148/255, blue: 204/255, alpha: 1.0)
            let confrigation = UISwipeActionsConfiguration(actions: [action])

            return confrigation
        } 
Vishal Vaghasiya
  • 4,618
  • 3
  • 29
  • 40