iOS TableView swipe UIContextualAction can not get a right image

你。 提交于 2019-12-13 03:34:32

问题


I want to set a image in the left swipe of cell, there is the code, but It doesn't work.

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
if (@available (iOS 11, *)) {
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        if (self.dataArray.count > indexPath.row) {
            [self.dataArray removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }

        completionHandler(YES);
    }];

    deleteAction.backgroundColor = [UIColor blueColor];

    UIContextualAction *backAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        // 不做任何事
        [tableView setEditing:NO animated:YES];
        completionHandler(YES);
    }];
    backAction.image = [UIImage imageNamed:@"public_system_success"];

    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, backAction]];

    return configuration;
}

return nil;

}

I got this left swipe menu

In fact the image is the same with cell image in the left. What's wrong?


回答1:


Use transparent back for the checkmark, because UIContextualAction renders images as templates, so any filled area of the image will appear white.

This should be your image:




回答2:


Check your image. I think the cell is applying a tint to your images when u swipe select them. Try this.. 1. Go to assets folder in xcode and select your image. 2. Go to Property Inspector on the right and select "render as" options and choose Original Image. 3. test.

Let me know if that helped :-)



来源:https://stackoverflow.com/questions/48535245/ios-tableview-swipe-uicontextualaction-can-not-get-a-right-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!