用于 UIContextMenuConfiguration 的 iOS 13.0 UIMenu 和 UIAction

C0D*_*0D3 5 ios swift ios13

我正在尝试使用 iOS 13.0 Beta 中引入的新 API。我已经下载了 Xcode 11.0 Beta 3 以便能够访问这些 API。

我在网上找到的一些代码执行以下操作:

extension SingleViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu<UIAction>? in
            // Creating Save button
            let save = UIAction(__title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill"), options: []) { action in
                // Just showing some alert
                self.showAlert(title: action.title)
            }

            // Creating Rotate button
            let rotate = UIAction(__title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), options: []) { action in
                self.showAlert(title: action.title)
            }
            // Creating Delete button
            let delete = UIAction(__title: "Delete", image: UIImage(systemName: "trash.fill"), options: .destructive) { action in
                self.showAlert(title: action.title)
            }
            // Creating Edit, which will open Submenu
            let edit = UIMenu<UIAction>.create(title: "Edit...", children: [rotate, delete])

            // Creating main context menu
            return UIMenu<UIAction>.create(title: "Menu", children: [save, edit])
        }
        return configuration
    }
}
Run Code Online (Sandbox Code Playgroud)

这看起来不错,但甚至无法在我的 Xcode 中编译。我得到的错误是:

Cannot specialize non-generic type 'UIMenu' Replace '<UIAction>' with ''

关于创建配置常量。

Type of expression is ambiguous without more context

关于创建保存操作。

以及其他类似的错误。

我还应该提到,我什至没有这种格式的构造函数:

UIAction(__title: "String", image: UIImage, options: Array)
UIMenu.create(...)
Run Code Online (Sandbox Code Playgroud)

为什么 Xcode-11 beta 3.0 中缺少这些?

mat*_*att 5

嗯,它变了。这是一个测试版!我希望它再次改变,但现在,在 beta 3 中, UIAction 的初始化程序是

init(__title title: String, image: UIImage?, identifier: UIAction.Identifier?, 
    handler: @escaping UIActionHandler)
Run Code Online (Sandbox Code Playgroud)

UIMenu 不是通用的,它的初始化器是

init(__title title: String, image: UIImage?, identifier: UIMenu.Identifier?, 
    options: UIMenu.Options = [], children: [UIMenuElement])
Run Code Online (Sandbox Code Playgroud)

因此,这是在 beta 3 下编译的代码的重写:

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu? in
        let save = UIAction(__title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill"), identifier: nil) { action in
            // whatever
        }
        let rotate = UIAction(__title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), identifier: nil) { action in
            // whatever
        }
        let delete = UIAction(__title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil) { action in
            // whatever
        }
        let edit = UIMenu(__title: "Edit", image: nil, identifier: nil, children:[rotate,delete])
        return UIMenu(__title: "Menu", image: nil, identifier: nil, children:[save, edit])
    }
    return configuration
}
Run Code Online (Sandbox Code Playgroud)