IOS11 UIContextualAction放置图像颜色文本

刘明鑫*_*刘明鑫 10 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;

}
Run Code Online (Sandbox Code Playgroud)

UnR*_*ewa 10

RSSReaderMaker解决方案可与少量更新Swift 5一起使用:

class ImageWithoutRender: UIImage {
    override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
        return self
    }
}
Run Code Online (Sandbox Code Playgroud)

并且在trailingSwipeActionsConfigurationForRowAtleadingSwipeActionsConfigurationForRowAt

使用:

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


小智 6

您可以通过修改一般改变图像的颜色tintColorUIImageUIAppearance,例如像这样:

UIImageView.appearance().tintColor = .yellow

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

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

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


wzb*_*zon 2

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

我建议使用MGSwipeTableCell.