UITableView -> trailingSwipeActionsConfigurationForRowAt -> UIContextualAction.image 对于 rowHeights < 50 没有正确居中

问题描述

我有一个 UITableView 的最后一列具有不同的行高。 每行都有 UIContextualAction 用于“标记为收藏”和“删除”,由图像(图标)表示。可以看出,当行高小于 50 时,UIContextualAction.image 放置已损坏且不再正确居中:

first row smaller then 50 height

func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        let (training,package) = decodeIndexPath(indexPath)           
        return package?.trainings.last == training ? 50 : 49
    }
    
    
 func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        guard let training = decodeIndexPath(indexPath).0 else { return nil }
        let imageSizeAction = CGSize(width: 30,height: 30)
        var actions = [UIContextualAction]()
        //add delete action (only custom exercises)
        if training.isCustom {
            let deleteAction = UIContextualAction(style: .destructive,title: nil) { [weak self] (action,view,completion) in
                guard let training = self?.decodeIndexPath(indexPath).0 else { return }
            Database.shared.deleteCustom(training: training,completion: logAsError)
                completion(true)
            }
            deleteAction.image = PaintCode.imageOfBtnCellActionDelete(imageSize: imageSizeAction)
            actions.append(deleteAction)
        }
        //add to favorites
        let favoriteAction = UIContextualAction(style: .normal,completion) in
            guard var training = self?.decodeIndexPath(indexPath).0 else { return }
            training.isFavorite = !training.isFavorite
            tableView.isEditing = false
            completion(true)
        }
        favoriteAction.backgroundColor = PaintCode.mainBlue
        let image = PaintCode.imageOfBtnCellActionFavorite(imageSize: imageSizeAction,selected: training.isFavorite)
        favoriteAction.image = image
        actions.append(favoriteAction)
        let action = UISwipeActionsConfiguration(actions: actions)
        //only allow full swipe if delete is added
        action.performsFirstActionWithFullSwipe = actions.contains(where: {$0.style == .destructive})
        return action
    }

解决方法

我尝试使用 backgroundColor 并制作 patternImage 颜色,我能够使其正确居中,但是由于平铺操作,当您拉伸滑动时,图标会重复。不是想要的行为

favoriteAction.backgroundColor = UIColor(patternImage: PaintCode.imageOfBtnCellActionFavorite(imageSize:imageSize,selected: training.isFavorite))

所以我看不出有其他选择,那就是将最小高度设为 50 点以确保一切工作可靠。