IOS11 UIContextualAction放置图像颜色文本

时间:2017-09-25 06:32:35

标签: objective-c ios11

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

- (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 个答案:

答案 0 :(得分:5)

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

UIImageView.appearance().tintColor = .yellow

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

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

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

答案 1 :(得分: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 :(得分:2)

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

我建议使用MGSwipeTableCell

答案 3 :(得分: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];

完成它。