如何在iOS中向上下文菜单添加自定义视图

5
我想在文本上方添加类似于iMessage的反应表情,就像这样:

enter image description here

目前,我只能显示菜单,不确定如何添加自定义视图在其上方。这是我的样子:

enter image description here

如何添加一个类似于iMessage反应视图的视图?
这是我在集合视图中创建上下文菜单的方法:
override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        let post = isFiltering ? filteredPosts[indexPath.section] : posts[indexPath.section]
        let identifier = NSString(string: "\(post.createdAt)")
        if post.username == "" {return nil}
        return UIContextMenuConfiguration(identifier: identifier, previewProvider: nil) { _ -> UIMenu? in
            let deleteAction = UIAction(title: "Delete", image: UIImage(systemName: "trash")) { _ in
                Service.deletePost(post: post)
            }
            let replyAction = UIAction(title: "Reply", image: UIImage(systemName: "arrowshape.turn.up.left")) { _ in
                self.setReplyingView(post: post)
            }
            
            let noteTagAction = UIAction(title: "Add Note Tag", image: UIImage(named: "noteFilterBlack")) { _ in
                DispatchQueue.main.async {
                    Service.addTag(named: "note", toPostWithId: post.id)
                }
            }
            
            let examTagAction = UIAction(title: "Add Exam Tag", image: UIImage(named: "examFilterBlack")) { _ in
                DispatchQueue.main.async {
                    Service.addTag(named: "exam", toPostWithId: post.id)
                }
            }
            
            let assignmentTagAction = UIAction(title: "Add Assignment Tag", image: UIImage(named: "assignmentFilterBlack")) { _ in
                DispatchQueue.main.async {
                    Service.addTag(named: "assignment", toPostWithId: post.id)
                }
            }
            
            let questionTagAction = UIAction(title: "Add Question Tag", image: UIImage(named: "questionFilterBlack")) { _ in
                DispatchQueue.main.async {
                    Service.addTag(named: "question", toPostWithId: post.id)
                }
            }
            guard let user = Service.shared.getUser() else { return UIMenu(title: "", children: [replyAction, deleteAction])}
            if user.uid == post.userId {
                return UIMenu(title: "", children: [replyAction, noteTagAction, examTagAction, assignmentTagAction, questionTagAction ,deleteAction])
            } else {
                return UIMenu(title: "", children: [replyAction, noteTagAction, examTagAction, assignmentTagAction, questionTagAction])
            }
        }
    }

然而,当返回UIContextMenuConfiguration时,我发现没有选项可以添加自定义视图。


1
你目前尝试了什么?请编辑您的帖子并包含代码。 - Rocket Nikita
刚刚更新了!如果需要添加更多细节,请告诉我。 - Sdanson
@Sdanson请查看我下面的答案。 - Waqas
@Sdanson,你是怎么做到的呢? - abhimuralidharan
2个回答

2

当您创建UIContextMenuConfiguration时,有一个参数需要使用闭包'previewProvider'。您可以返回自定义视图。以下是示例。

UIContextMenuConfiguration(identifier: "unique-id", previewProvider: {
        let customView = CustomViewController()
        return customView
    }, actionProvider: {
        
    })

请注意,您提供的链接并不与上面的问题相符。 - famfamfam
我已经使用这个解决方案来实现ContextMenu上的自定义视图。如果你不能检查它,至少让问题的所有者测试一下。 - Waqas
抱歉,我在3月14日回答您的问题时可能有些愚蠢,那一天我因为被困在自定义上下文菜单中整整一天没有睡觉,我的思维不像平常一样清晰(13->14),后来我查看了您给出的答案,发现它是最佳解决方案。请原谅我,谢谢。 - famfamfam
1
我可以问你一个问题吗?当我调试查看层次结构时,我发现预览提供程序被苹果设计的视图遮挡以获取手势事件,因此该提供程序无法获取任何触摸事件。那么你是如何解决这个问题的?我需要获取触摸事件来实现像这个页面问题(作为消息应用程序)的反应。 - famfamfam
是的,我测试过了,您可以在此处双重检查我的帖子 https://stackoverflow.com/questions/66631995/swift-custom-context-menu-previewprovider-can-not-click-any-view-insideusing-ta - famfamfam
显示剩余2条评论

0

https://github.com/Hungs20/Reaction-message-ContextMenu。这是我的解决方案,使用UITargetedPreview显示自定义视图

@available(iOS 13.0, *)
func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    // makeTargetedPreview
}

@available(iOS 13.0, *)
func tableView(_ tableView: UITableView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    // makeTargetedDismissPreview
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接