iOS 14 从 UIView 呈现 UIMenu

Joe*_*her 7 ios uimenu ios14

iOS 14UIMenu似乎可以从 anyUIBarButtonItemUIButton/呈现UIControl,但我如何从通用 呈现它UIView

Ton*_*enu 3

添加交互:

\n
let interaction = UIContextMenuInteraction(delegate: self)\nmenuView.addInteraction(interaction)\n
Run Code Online (Sandbox Code Playgroud)\n

将 UIContextMenuInteractionDelegate 添加到您的控制器:

\n
extension YourViewController: UIContextMenuInteractionDelegate {\n               \n      func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {\n        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in\n            \n            // Create an action for sharing\n            let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { action in\n                // Show system share sheet\n            }\n    \n            // Create an action for renaming\n            let rename = UIAction(title: "Rename", image: UIImage(systemName: "square.and.pencil")) { action in\n                // Perform renaming\n            }\n    \n            // Here we specify the "destructive" attribute to show that it\xe2\x80\x99s destructive in nature\n            let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { action in\n                // Perform delete\n            }\n    \n            // Create and return a UIMenu with all of the actions as children\n            return UIMenu(title: "", children: [share, rename, delete])\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

现在,长按视图并查看您的菜单:)

\n

更多这里: https: //kylebashour.com/posts/context-menu-guide

\n

  • 如果不完全清楚,我很抱歉,我希望从通用的“UIView”中呈现菜单,而无需长按。 (9认同)