IOS11 UIContextualAction放置图像颜色文本

问题描述 投票:8回答:4

为了防止图片如何恢复真实的颜色我现在放置的图像是白色的

- (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;

}
objective-c ios11
4个回答
6
投票

您可以通过tintColor修改UIImage的常规UIAppearance来更改图像的颜色,例如:

UIImageView.appearance().tintColor = .yellow

但这会将(默认)色调颜色应用于应用中的所有图像视图,因此您可以通过以下方式设置将其限制为表格视图中的图像视图:

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

如果有的话,你也可以指定你自己的UITableView子类。


3
投票

RSSReaderMaker解决方案使用小更新Swift 5:

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

trailingSwipeActionsConfigurationForRowAtleadingSwipeActionsConfigurationForRowAt

使用:

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

2
投票

trailingSwipeActionsConfigurationForRowAtIndexPath仅适用于iOS 11,完全无法自定义。您可以更改按钮的背景颜色,但即使使用UIImageRenderingModeAlwaysOriginal也无法使用自己的颜色添加图像。

我推荐使用MGSwipeTableCell


2
投票

我解决了这个问题。第一步是使用随机名称声明UIImage的子类,并覆盖其imageWithRenderingMode:方法。我想做这个

@implementation FakeImage

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

第二步,在为UIContextualAction赋值时,使用这个类,注意用initWithCGImage方法实例化。

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

把它做完。

© www.soinside.com 2019 - 2024. All rights reserved.