IOS11 UIContextualAction Put image color text

To prevent the image How to restore the real color Now I put the image in 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; } 
+7
source share
4 answers

You can change the color of the image by changing the general tintColor UIImage through UIAppearance , for example:

UIImageView.appearance().tintColor =.yellow

but this applies to the hue color (default) for all types of images in your application, so you can limit it to viewing images in a table, setting it this way:

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

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

+4
source

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

I recommend using MGSwipeTableCell .

+2
source

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 it

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

In the second step, when assigning UIContextualAction values, use this class, pay attention to creating an instance using the initWithCGImage method.

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

Do this.

+2
source

RSSReaderMaker works with minor Swift 5 updates:

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

And in trailingSwipeActionsConfigurationForRowAt or leadingSwipeActionsConfigurationForRowAt

Using:

 if let cgImageX = UIImage(named: "x_blue_big")?.cgImage { action.image = ImageWithoutRender(cgImage: cgImageX, scale: UIScreen.main.nativeScale, orientation: .up) } 
0
source

Source: https://habr.com/ru/post/1272085/


All Articles