首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 14呈现来自UIView的UIMenu

iOS 14呈现来自UIView的UIMenu
EN

Stack Overflow用户
提问于 2021-01-05 02:42:25
回答 1查看 519关注 0票数 4

iOS 14的UIMenu似乎能够从任何UIBarButtonItemUIButton / UIControl中呈现,但我如何从通用UIView中呈现它

EN

回答 1

Stack Overflow用户

发布于 2021-01-07 02:34:51

添加交互组件:

代码语言:javascript
复制
let interaction = UIContextMenuInteraction(delegate: self)
menuView.addInteraction(interaction)

将UIContextMenuInteractionDelegate添加到您的控制器:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65568076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档