首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在swift中点击按钮即可显示上下文菜单

在swift中点击按钮即可显示上下文菜单
EN

Stack Overflow用户
提问于 2021-01-20 18:44:06
回答 1查看 2.6K关注 0票数 3

在iOS中,可以通过长按或轻触来显示上下文菜单。目前,下面的代码显示了长按下的上下文菜单,我如何在轻触上显示菜单?

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

回答 1

Stack Overflow用户

发布于 2021-02-02 22:54:23

UIContextMenuInteraction仅适用于上下文(长按)菜单。

如果希望按钮的主要操作显示菜单,只需创建一个UIMenu并将其直接分配给button.menu属性,然后设置button.showsMenuAsPrimaryAction = true,如下所示:

代码语言:javascript
复制
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, ...])
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65807946

复制
相关文章

相似问题

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