在iOS中,可以通过长按或轻触来显示上下文菜单。目前,下面的代码显示了长按下的上下文菜单,我如何在轻触上显示菜单?
let interaction = UIContextMenuInteraction(delegate: self)
tagBtn.addInteraction(interaction)
func contextMenuInteraction(_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint)
-> UIContextMenuConfiguration? {
let favorite = UIAction(title: "Favorite",
image: UIImage(systemName: "heart.fill")) { _ in
// Perform action
}
let share = UIAction(title: "Share",
image: UIImage(systemName: "square.and.arrow.up.fill")) { action in
// Perform action
}
let delete = UIAction(title: "Delete",
image: UIImage(systemName: "trash.fill"),
attributes: [.destructive]) { action in
// Perform action
}
return UIContextMenuConfiguration(identifier: nil,
previewProvider: nil) { _ in
UIMenu(title: "Actions", children: [favorite, share, delete])
}
}发布于 2021-02-02 22:54:23
UIContextMenuInteraction仅适用于上下文(长按)菜单。
如果希望按钮的主要操作显示菜单,只需创建一个UIMenu并将其直接分配给button.menu属性,然后设置button.showsMenuAsPrimaryAction = true,如下所示:
let favorite = UIAction(title: "Favorite",
image: UIImage(systemName: "heart.fill")) { _ in
// Perform action
}
...
let button = UIButton()
button.showsMenuAsPrimaryAction = true
button.menu = UIMenu(title: "", children: [favorite, ...])https://stackoverflow.com/questions/65807946
复制相似问题