IOS11 UIContextualAction Place the image color text

Viewed 5382

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;

}
4 Answers

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)
}

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.

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.

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.

Related