iOS 14 从 UIView 呈现 UIMenu
Posted
技术标签:
【中文标题】iOS 14 从 UIView 呈现 UIMenu【英文标题】:iOS 14 present UIMenu from UIView 【发布时间】:2021-04-10 13:33:15 【问题描述】:ios 14 的UIMenu
似乎可以从任何UIBarButtonItem
或UIButton
/ UIControl
呈现,但我将如何从通用UIView
呈现它?
【问题讨论】:
【参考方案1】:添加交互:
let interaction = UIContextMenuInteraction(delegate: self)
menuView.addInteraction(interaction)
将 UIContextMenuInteractionDelegate 添加到您的控制器:
extension YourViewController: UIContextMenuInteractionDelegate
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration?
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) suggestedActions in
// Create an action for sharing
let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) action in
// Show system share sheet
// Create an action for renaming
let rename = UIAction(title: "Rename", image: UIImage(systemName: "square.and.pencil")) action in
// Perform renaming
// Here we specify the "destructive" attribute to show that it’s destructive in nature
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) action in
// Perform delete
// Create and return a UIMenu with all of the actions as children
return UIMenu(title: "", children: [share, rename, delete])
现在,长按视图并查看您的菜单 :)
更多:https://kylebashour.com/posts/context-menu-guide
【讨论】:
如果没有完全清楚,我深表歉意,我希望通过一个通用的UIView
呈现菜单没有长按。以上是关于iOS 14 从 UIView 呈现 UIMenu的主要内容,如果未能解决你的问题,请参考以下文章
iOS 如何从 Uiview 呈现 Modal ViewController
如何修改 UIBarButtonItem 和 UIButton 的 UIMenu