iOS 14的UIMenu似乎能够从任何UIBarButtonItem或UIButton / UIControl中呈现,但我如何从通用UIView中呈现它
发布于 2021-01-07 02:34:51
添加交互组件:
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://stackoverflow.com/questions/65568076
复制相似问题