首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于rowHeights < 50,UITableView -> trailingSwipeActionsConfigurationForRowAt -> UIContextualAction.image未正确居中

对于rowHeights < 50,UITableView -> trailingSwipeActionsConfigurationForRowAt -> UIContextualAction.image未正确居中
EN

Stack Overflow用户
提问于 2021-01-31 19:59:22
回答 1查看 65关注 0票数 0

我有一个UITableView,它的最后一列有不同的行高。每一行都有“标记为收藏”和“删除”的UIContextualAction,由图像(图标)表示。它表明,当行高小于50时,UIContextualAction.image放置被破坏,不再正确居中:

代码语言:javascript
复制
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
    }
EN

回答 1

Stack Overflow用户

发布于 2021-01-31 20:01:48

我试着使用backgroundColor和一个patternImage颜色,我能够让它正确地居中,然而,由于平铺操作,当你伸展滑动时,你会得到重复的图标。不是想要的行为

代码语言:javascript
复制
favoriteAction.backgroundColor = UIColor(patternImage: PaintCode.imageOfBtnCellActionFavorite(imageSize:imageSize, selected: training.isFavorite))

因此,我没有看到其他选择,然后有50点的最低高度,使一切都可靠地工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65978834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档