微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 14 从 UIView 呈现 UIMenu

如何解决iOS 14 从 UIView 呈现 UIMenu

iOS 14 的 UIMenu 似乎可以从任何 UIBarButtonItemUIButton / UIControl 呈现,但我将如何从通用 {{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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。