iOS 13.0 UIMenu和用于UIContextMenuConfiguration的UIAction

时间:2019-07-09 20:54:35

标签: 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
    }
}

这似乎很好,但是甚至没有在我的Xcode中编译。我得到的错误是:

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

关于创建配置常量。

Type of expression is ambiguous without more context

关于创建保存操作。

以及其他一些类似的错误。

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

UIAction(__ title:“字符串”,image:UIImage,选项:Array) UIMenu.create(...)

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

我们将不胜感激。

3 个答案:

答案 0 :(得分:3)

好吧,它变了。这是一个测试版!我希望它会再次发生变化,但是就目前而言,在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
}

答案 1 :(得分:0)

在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:...)。现在它们都是真正的初始值设定者了,

答案 2 :(得分:0)

在 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
  }
}