4

I am using UIContextualAction to implement swipe to delete functionality for UITableViewCell. I use + (instancetype)contextualActionWithStyle:(UIContextualActionStyle)style title:(nullable NSString *)title handler:(UIContextualActionHandler)handler method to construct UIContextualAction and later set an image to it using setImage: property.

However when I run this code and try to swipe delete, only image is shown in the menu. The title is missing. When I comment out the setImage property, title is displayed. Why is this so? Am I missing something in the implementation or is this a bug with Apple?

Advaith
  • 1,087
  • 3
  • 12
  • 31

2 Answers2

15

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

EDIT

My answer was based on my own experience, unfortunately the official docs for that particular class are of no help at all. I also found a hint on the issue here ("By specifying an image, the title in the initializer is not displayed").

In your comment you say that if the height is >91px, both title and image are shown. That matches this radar (there it says that the height should be >=91px), so it might be a bug after all.

Looks like for the moment, the only options are to either create images with embedded text, or increase the cell height to 91px.

EDIT 2 As pointed out by laka's comment, using an image with embedded text has accessibility drawback, e.g. VoiceOver will not be able to identify the semantics of the action. You can use accessibilityCustomActions to overcome this problem (see this question).

dr_barto
  • 5,723
  • 3
  • 26
  • 47
1

As a solution we could render text into image using UIGraphicsImageRenderer

let renderer = UIGraphicsImageRenderer(bounds: bounds)
contextAction.image = renderer.image { context in
    // draw your image and text using Core Graphics API here
}

It is workaround, but Apple doesn't provide us another way to do it ...

A. Poltoratskyi
  • 410
  • 6
  • 13