【问题标题】:ContextMenuConfigurationForRowAt in iOS13iOS13 中的 ContextMenuConfigurationForRowAt
【发布时间】:2019-09-20 17:12:57
【问题描述】:

在这个WWDC19赛季的视频Modernizing Your UI for iOS 13中,这个方法是创建一个上下文菜单,但是我在使用时出错:

@available(iOS 13.0, *)
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    let actionProvider = (suggestedActions: [UIMenuElement])-> UIMenu? // in this line i got an error {
        let editMenu = UIMenu(title: "Edit...", children: [
        UIAction(title: "Copy") {},
        UIAction(title: "Duplicate") {}
        ])
        return UIMenu(children: [
        UIAction(title: "Share") {},
        editMenu,
        UIAction(title: "Delete", style: .destructive) {}
        ])
    }

    return UIContextMenuConfiguration(identifier: "unique-ID" as NSCopying,
                                      previewProvider: nil,
                                      actionProvider: actionProvider)
}

错误出现在-> UIMenu? 行并显示Expected type after '->'。谁能帮我解决一下?

【问题讨论】:

标签: swift contextmenu ios13


【解决方案1】:

你有很多语法错误:

func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    let actionProvider: UIContextMenuActionProvider = { _ in
        let editMenu = UIMenu(title: "Edit...", children: [
            UIAction(title: "Copy") { _ in },
            UIAction(title: "Duplicate") { _ in }
        ])
        return UIMenu(title: "Title", children: [
            UIAction(title: "Share") { _ in },
            editMenu
        ])
    }

    return UIContextMenuConfiguration(identifier: "unique-ID" as NSCopying, previewProvider: nil, actionProvider: actionProvider)
}

请注意,有些 API 在 WWDC 之后发生了变化,您应该考虑更新它们,类似于上面的代码。您可以查看由 Kyle Bashour 撰写的综合 Guide to iOS Context Menus

例子:

func makeContextMenu() -> UIMenu {
    let rename = UIAction(title: "Rename Pupper", image: UIImage(systemName: "square.and.pencil")) { action in
        // Show rename UI
    }

    // Here we specify the "destructive" attribute to show that it’s destructive in nature
    let delete = UIAction(title: "Delete Photo", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
        // Delete this photo ?
    }

    // The "title" will show up as an action for opening this menu
    let edit = UIMenu(title: "Edit...", children: [rename, delete])

    let share = UIAction(...)

    // Create our menu with both the edit menu and the share action
    return UIMenu(title: "Main Menu", children: [edit, share])
}

func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in

        // "puppers" is the array backing the collection view
        return self.makeContextMenu(for: self.puppers[indexPath.row])
    })
}

【讨论】:

  • 如何更改按钮标题字体?
猜你喜欢
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多