Есть ли в iOS 13 - UIMenu ошибка, которая не отображает его изображение? - PullRequest
0 голосов
/ 17 октября 2019

Вставьте следующий код в проект:

Изображение не отображается рядом с «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])
    }
}

enter image description here

В демонстрационной программе Apple WWDC они могут сделать это, как показано ниже:

enter image description here

1 Ответ

1 голос
/ 17 октября 2019

Контекстное меню состоит из двух частей: предварительного просмотра и меню. Оба являются необязательными. На скриншоте Apple «продуктовые» продукты - это preview , а не меню. По умолчанию ячейка снимается моментально, и снимок отображается в качестве предварительного просмотра. Само меню на скриншоте Apple не имеет изображения и заголовка. И это то, что вы тоже должны делать! Последняя строка

return UIMenu(...

... не должна иметь заголовка и изображения. Это меню, которое оборачивает все остальное и возвращается, является меню верхнего уровня , и оно отображается по-другому (как показывает ваш собственный скриншот). Лучше всего выглядит без заголовка и вообще не может отображать изображение. Его работа заключается в том, чтобы обернуть все остальное и предоставить идентификатор, и это все.

Затем вы получите что-то вроде этого:

enter image description here

...