How can I use suggestedActions given from UIContextMenuConfiguration?

391 Views Asked by At

Other than the a WWDC video - 44:18 a sample project there's not much info online.

So the actionProvider, when called, has a list of suggestedActions that are passed to it by the system.

This can be a mix of UIMenus and UIActions, so potentially a fully constructed hierarchy that are picked up from the system. These could be things that you defined in your responder chain using the new UI command API being introduced in iOS 13 or things that are offered by other system components. So we're doing a fully custom menu here, so we're going to put the suggestedActions aside. First, we'll create our edit menu.

But so far what I've learned is that a given Identifier can have pre-defined suggestedActions the suggestionActions are not associated with any identifier. If I change that identifier the suggestedActions won't change. It seems to be based on its context, which here I'm guessing I'm in the context of a tableviewcell...

I'm trying to see what it looks like, but nothing shows up. If I tap on the cell nothing shows up.

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
        })
    }
}

I've also printed suggestedActions, what I got out was:

  - 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

There are 0 best solutions below