1

I have a UITableView that has a different row height for the last column. Each row has UIContextualAction to "mark as favourite" and "delete", represented by image (icons). It seams that when the row height is smaller then 50, the UIContextualAction.image placement is corrupted and no longer centered correctly:

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, title: nil) { [weak self] (action, view, 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
    }
4

1 回答 1

0

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

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

所以我认为没有其他选择,然后将最小高度设为 50 点以使一切工作可靠。

于 2021-01-31T12:01:47.510 回答