gpt4 book ai didi

iOS 14 从 UIView 呈现 UIMenu

转载 作者:行者123 更新时间:2023-12-04 13:54:12 33 4
gpt4 key购买 nike

iOS 14 UIMenu似乎可以从任何 UIBarButtonItem 呈现或 UIButton/UIControl ,但我将如何从通用 UIView 中呈现它?

最佳答案

添加交互:

let interaction = UIContextMenuInteraction(delegate: self)
menuView.addInteraction(interaction)
将 UIContextMenuInteractionDelegate 添加到您的 Controller :
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])
}
}
}
现在,长按 View 并查看您的菜单:)
更多信息: https://kylebashour.com/posts/context-menu-guide

关于iOS 14 从 UIView 呈现 UIMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65568076/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com