iOS 13 - UIMenu 是否存在不显示其图像的错误?

Does iOS 13 - UIMenu have a bug that doesn't show its image?

将以下代码粘贴到项目中:

'Device Honey' 旁边没有显示图像,即 UIMenu 但是图像显示在 'Copy' 旁边,即 UIACtion.

我是不是做错了什么?如果这是一个错误?有解决方法吗?

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

            return self.makeContextMenu(for: indexPath)
        })
    }

    @available(iOS 13.0, *)
    func makeContextMenu(for indexPath: IndexPath) -> UIMenu? {

        let copyAction = UIAction(title: "Copy", image: UIImage(systemName: "square.and.arrow.up")) { [weak self] _ in
            guard let self = self else { return }
            let cell = self.tableview.cellForRow(at: indexPath)
            let pasteboard = UIPasteboard.general
            pasteboard.string = cell?.detailTextLabel?.text
        }

        guard let cell = self.tableview.cellForRow(at: indexPath), let title = cell.textLabel?.text else { return nil}
        return UIMenu(title: "Device \(title) ", image: UIImage(systemName: "square.and.arrow.up"), children: [copyAction])
    }
}

在 Apple 的 WWDC 演示中,他们能够做到这一点,如下所示:

上下文菜单有两部分:预览和菜单。两者都是可选的。 Apple 屏幕截图中的 "groceries" 是 预览 ,而不是菜单。默认情况下,对单元格进行快照并将快照显示为预览。 Apple 屏幕截图中的菜单本身没有图像也没有标题。这也是你应该做的!最后一行

return UIMenu(...

...应该没有标题和图像。此菜单是 top-level 菜单,它包含所有其他内容并返回,显示方式不同(如您自己的屏幕截图所示)。它没有标题看起来最好,而且根本无法显示图像。它的工作是包装其他所有内容并提供标识符,仅此而已。

然后你会得到这样的东西:

这条评论比较重要

您在问题中显示的预览非常具有误导性。它看起来像菜单中的其他项目。然而,您屏幕截图中的杂货部分是 预览。一个更有特色的预览会是这样的:

Image source

我也有同样的问题,关于 UIMenu init(title:image:identifier:options:children:) 的文档表示一个图像参数:

image: The image to display next to the menu's title.

它看起来像是要用在子菜单中,如这篇博客所示: UIMenu Comprehensive Guide