如何使用UIContextMenuConfiguration中给出的建议动作?

时间:2019-10-18 18:46:58

标签: ios uitableview ios13 uiaction uimenu

除了WWDC video - 44:18sample project之外,在线信息很少。

  

因此,actionProvider在被调用时具有suggestedActions的列表   由系统传递给它。

     

这可以是UIMenuUIAction的混合,因此可能是完全   从系统中拾取的构造层次结构。这些可以   是您使用新用户界面在响应者链中定义的事物   iOS 13中引入的命令API或   其他系统组件。所以我们在这里做一个完全自定义的菜单,所以   我们将suggestedActions放在一边。首先,我们将创建   我们的编辑菜单。

但是到目前为止,我了解到的是给定标识符可以具有预定义的uggestedActions。 suggestionActions与任何标识符都没有关联。如果我更改该标识符,则suggestedActions不会更改。它似乎基于它的上下文,在这里我猜我是在tableviewcell的上下文中...

我正在尝试查看它的外观,但是什么都没有显示。如果我点击单元格,则不会显示任何内容。

class ViewController: UIViewController {
    let tableview: UITableView = {
        let tv = UITableView()
        tv.frame = UIScreen.main.bounds

        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableview)
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if cell.detailTextLabel == nil {
            cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        }
        cell.textLabel?.text = "Honey"
        cell.detailTextLabel?.text = "iOS developer"

        return cell
    }

    @available(iOS 13.0, *)
    func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {

        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
            print("suggestedActions", suggestedActions)
            let menu = UIMenu(title: "", children: suggestedActions)

            return menu
        })
    }
}

我还打印了suggestedActions,结果是:

  - 0 : <UIMenu: 0x6000008e6790; title = Standard Edit; identifier = com.apple.menu.standard-edit; options = (Inline); children = <NSArray: 0x600001318900>>
  - 1 : <UIMenu: 0x6000008e6820; title = Replace; identifier = com.apple.menu.replace; options = (Inline); children = <NSArray: 0x6000008e64c0>>
  - 2 : <UIMenu: 0x6000008e7330; title = Text Style; identifier = com.apple.menu.text-style; image = <UIImage:0x60000349c870 symbol(system: bold.italic.underline) {36, 17} baseline=3.66667,contentInsets={1, 2, 1, 2},alignmentRectInsets={-2.9999999999999982, 0, -0.33333333333333348, 0} config=<(null), traits=(UserInterfaceIdiom = Phone, DisplayScale = 3, DisplayGamut = P3, HorizontalSizeClass = Compact, VerticalSizeClass = Regular, UserInterfaceStyle = Light, UserInterfaceLayoutDirection = LTR, PreferredContentSizeCategory = L)>>; children = <NSArray: 0x6000008e6df0>>
  - 3 : <UIMenu: 0x6000008e6d00; title = Lookup; identifier = com.apple.menu.lookup; options = (Inline); children = <NSArray: 0x60000049c870>>
  - 4 : <UIMenu: 0x6000008e7060; title = Learn; identifier = com.apple.menu.learn; options = (Inline); children = <NSArray: 0x60000049c220>>
  - 5 : <UIMenu: 0x6000008e6d90; title = Speech; identifier = com.apple.command.speech; options = (Inline); children = <NSArray: 0x6000008e6490>>
  - 6 : <UIMenu: 0x6000008e7180; title = Share; identifier = com.apple.menu.share; options = (Inline); children = <NSArray: 0x60000049c6a0>>
  - 7 : <UIMenu: 0x6000008e6a30; title = Writing Direction; identifier = com.apple.menu.writing-direction; options = (Inline); children = <NSArray: 0x6000006fe1c0>>

0 个答案:

没有答案