11

To prevent the picture How to restore the real color I now place the image are white

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0))
{

 UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL))
    {
        NSLog(@"------------------>%@",sourceView);
        completionHandler (YES);
    }];

    deleteRowAction.image = [UIImage imageNamed:@"heart"];

    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
         return config;


    return nil;

}
刘明鑫
  • 111
  • 1
  • 4

4 Answers4

15

RSSReaderMaker solution work with small updates Swift 5:

class ImageWithoutRender: UIImage {
    override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
        return self
    }
}

And in trailingSwipeActionsConfigurationForRowAt or leadingSwipeActionsConfigurationForRowAt

Use:

if let cgImageX =  UIImage(named: "x_blue_big")?.cgImage {
     action.image = ImageWithoutRender(cgImage: cgImageX, scale: UIScreen.main.nativeScale, orientation: .up)
}
UnRewa
  • 2,462
  • 2
  • 29
  • 31
6

You can change the color of the image by modifying the general tintColor of UIImage via UIAppearance, for example like this:

UIImageView.appearance().tintColor = .yellow

but this applies that (default) tint color to all image views in your app, so you might limit it to image views within a table view by setting it this way:

UIImageView.appearance(whenContainedInInstancesOf: [UITableView.self]).tintColor = .yellow

You can also specify your own UITableView subclass if you have one.

Andi
  • 101
  • 1
  • 4
4

I solved this problem. The first step is to declare a subclass of UIImage with a random name and override its imageWithRenderingMode: method. I want to do this

@implementation FakeImage

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
{
     return self;
}
@end

The second step, when assigning values to UIContextualAction, use this class, pay attention to instantiate with initWithCGImage method.

UIContextualAction* action = xxxx;
action.image = [[FakeImage alloc] initWithCGImage:[UIImage imageNamed:@"zoo_icon_time"].CGImage scale:2 orientation:UIImageOrientationUp];

Get it done.

  • This solution is half worked for me, it will also change the size of the image. – Hemang Apr 08 '19 at 07:31
  • 1
    I guess there was a problem with the step of regenerating the UIImage, try trying to adjust the scale parameter. [[FakeImage alloc] initWithCGImage:[UIImage imageNamed:@"zoo_icon_time"].CGImage scale:*2(here)* orientation:UIImageOrientationUp]; – RSSReaderMaker Apr 10 '19 at 06:55
  • This worked perfectly for me, but watch out for that scale. – Pan Mluvčí May 29 '20 at 02:12
2

trailingSwipeActionsConfigurationForRowAtIndexPath works from iOS 11 only and is completely not customizable. You can change background color of a button, but you cannot add an image with you own colors even if you use UIImageRenderingModeAlwaysOriginal.

I recommend using MGSwipeTableCell.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70