Swift iOS 13-UIMenu是否有一个不';你不展示它的形象吗?
将以下代码粘贴到项目中: “设备蜂蜜”旁边没有显示图像,即Swift iOS 13-UIMenu是否有一个不';你不展示它的形象吗?,swift,uitableview,ios13,uimenu,uiaction,Swift,Uitableview,Ios13,Uimenu,Uiaction,将以下代码粘贴到项目中: “设备蜂蜜”旁边没有显示图像,即ui菜单 但是,图像显示在“复制”旁边,即ui操作 我做错什么了吗?如果这是一个错误?有解决办法吗 class ViewController: UIViewController { let tableview: UITableView = { let tv = UITableView() tv.frame = UIScreen.main.bounds return tv }
ui菜单
但是,图像显示在“复制”旁边,即ui操作
我做错什么了吗?如果这是一个错误?有解决办法吗
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])
}
}
在苹果的WWDC演示中,他们能够做到如下所示:
上下文菜单有两部分:预览和菜单。两者都是可选的。苹果截图中的“杂货”是预览,而不是菜单。默认情况下,该单元为快照,快照显示为预览。苹果截图中的菜单本身没有图像和标题。这也是你应该做的!最后一行
return UIMenu(...
…应该没有标题和图像。此菜单是顶级菜单,它包装了所有其他内容并返回,显示方式不同(如您自己的屏幕截图所示)。它没有标题看起来最好,而且根本无法显示图像。它的工作是包装所有其他内容并提供一个标识符,仅此而已
然后,您将得到如下结果:
这是一个更重要的评论
你在问题中展示的预览非常误导人。它看起来像菜单中的其他项目。然而,屏幕截图中的杂货部分是一个预览版。更具特色的预览将如下所示:
那么苹果是如何制作“杂货”标题和图片的?很明显,我的标题显示了…“最高水平”,这是关键。API中有提到过吗?你能在你的答案中包括这一点吗?我想没有内置的类,我可以只分配一个标题和图片。对吧?我不知道那是什么意思。你是说你想自己创建预览?当然可以,我想问的是,如果像tableviewcell一样,你只需转储图像和文本,布局就完成了,PreviewProvider是否也有类似的功能?我宁愿不自己创造:)