iOS开发之UIMenu

yungfan· 2020-07-26

iOS 13 引入

UIMenu 在 iOS 13 中引入,可以很方便的创建程序菜单和上下文菜单。

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        navigationController?.isToolbarHidden = false
        // 菜单绑定到UIBarButtonItem(iOS 14的构造函数)
        let addNewItem = UIBarButtonItem(systemItem: .add, primaryAction: nil, menu: createMenuIOS13()) 
        // 放到工具条       
        toolbarItems = [addNewItem]
    }

    func createMenuIOS13() -> UIMenu {
        // 第一个菜单
        let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { _ in
            print("favorite")
        }
        // 第二个菜单
        let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { _ in
            print("share")
        }
        // 第三个菜单
        let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: [.destructive]) { _ in
            print("delete")
        }

        let menuActions = [favorite, share, delete]

        let addNewMenu = UIMenu(
            title: "",
            children: menuActions)

        return addNewMenu
    }
}

参考:iOS开发之Context Menus

iOS 14 增强

iOS 14 中引入UIDeferredMenuElement,允许异步地创建 UIMenu,也就是说可以动态在后台配置菜单的内容。

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // 放到导航条
        navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .add, primaryAction: nil, menu: createMenuIOS14())
    }

    func createMenuIOS14() -> UIMenu {
        // 应该是是通过网络获取,这里直接从Bundle加载
        let menuItemsForUser = Bundle.main.decode([RemoteItem].self, from: "menu.json")

        // 创建UIDeferredMenuElement
        let dynamicElements = UIDeferredMenuElement { completion in
            // 创建UIAction
            let actions = menuItemsForUser.map { item in
                UIAction(title: item.title, image: UIImage(systemName: item.icon)) { _ in
                    print("\(item.title) tapped")
                }
            }

            // 一定要调用completion处理
            completion(actions)
        }

        return UIMenu(
            title: "",
            children: [dynamicElements])
    }
}

// 菜单Model
struct RemoteItem: Codable {
    let title: String
    let icon: String
}

// 加载文件并转Model
extension Bundle {
    func decode<T: Decodable>(_ type: T.Type, from file: String) -> T {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Failed to find \(file) in bundle.")
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }

        let decoder = JSONDecoder()

        guard let model = try? decoder.decode(T.self, from: data) else {
            fatalError("Failed to decode \(file) from bundle.")
        }

        return model
    }
}

JSON文件内容如下:

[
    {
        "title": "Favorite",
        "icon": "heart.fill"
    },
    {
        "title": "Share",
        "icon": "square.and.arrow.up.fill"
    },
    {
        "title": "Delete",
        "icon": "trash.fill"
    }
]

效果

源代码

UIMenu案例

我的微信公众号

定期发布 Swift、SwiftUI、Combine、iOS开发等技术文章,也会更新一些自己的学习心得,欢迎大家关注。