gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:53:55 24 4
gpt4 key购买 nike

我想像这样在文本上方添加像 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com