ios - 如何将自定义 View 添加到上下文菜单 iOS

标签 ios swift uicollectionview contextmenu

我想像这样在文本上方添加像 iMessage 这样的 react :

enter image description here

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

enter image description here

如何添加类似于 iMessage react View 的 View ?

这就是我在 Collection View 中创建上下文菜单的方式:

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 时,我看不到添加自定义 View 的选项。

最佳答案

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

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

关于ios - 如何将自定义 View 添加到上下文菜单 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65924726/

相关文章:

ios - 可折叠部分 : [Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0)

ios - UIcollectionview装饰 View VS补充 View

swift - 如何根据标签文本快速更改 CollectionView 单元格宽度

android - RAD跨平台开发建议

ios - 检索 NSManagedObjects 问题(核心数据)

ios - 继续引用

ios - Swift 是否可以继承轻量级通用 Objective-c 类?

ios - 如何从后端的 mp3 文件 URL 获取元数据以显示 ArtWork 图像?

arrays - 数组的 Swift NSUserDefaults setObject

iOS 如何为来自 CollectionView 的图像上的 UIPreviewActions 确认 'Delete'