如何将自定义视图添加到 iOS 上下文菜单

Sda*_*son 5 contextmenu ios uicollectionview swift

我想在文本上方添加像 iMessage 这样的反应,如下所示:

在此输入图像描述

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

在此输入图像描述

如何添加与 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])
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,当返回 UIContextMenuConfiguration 时,我看不到添加自定义视图的选项。

Waq*_*qas 2

当您创建 UIContextMenuConfiguration 时,这里有一个参数,该参数在此处采用闭包作为“previewProvider”,您可以返回自定义视图。下面是一个例子..

UIContextMenuConfiguration(identifier: "unique-id", previewProvider: {
        let customView = CustomViewController()
        return customView
    }, actionProvider: {
        
    })
Run Code Online (Sandbox Code Playgroud)