iOS 13.0 UIMenu 和 UIContextMenuConfiguration 的 UIAction

iOS 13.0 UIMenu and UIAction for UIContextMenuConfiguration

我正在尝试使用 iOS 13.0 Beta 中引入的新 APIs。我已经下载 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
    }
}

这看起来不错,但在我的 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(...)

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

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

init(__title title: String, image: UIImage?, identifier: UIAction.Identifier?, 
    handler: @escaping UIActionHandler)

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

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

所以这是在 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
}

在 Xcode 11.0 Beta 5 中,您现在可以写:

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

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

            // Creating main context menu
            return UIMenu(title: "Menu", children: [save, edit])
        }
        return configuration
    }
}
  • 类 不再通用,例如UIMenu 而不是 UIMenu<UIAction>.
  • 全面的 Swift 初始化程序让您可以删除可选参数,这些参数具有合理的默认值,例如imageattributesidentifier
  • 不再有 UIMenu<UIAction>.create(...)UIAction(__title:...)。他们现在都是真正的初始化程序,耶!

在 swift 5 上,这有效:

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

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

        // Creating main context menu
        return UIMenu(title: "Menu", children: [save, edit])
    }
    return configuration
  }
}