How do I add a toolbar to a macOS app using SwiftUI?

前端 未结 4 1692
余生分开走
余生分开走 2021-02-05 15:32

I am trying to add a toolbar inside the title bar to a macOS app using SwiftUI, something similar to what is shown below.

I am unable to figure out a way to ac

4条回答
  •  臣服心动
    2021-02-05 16:10

    UIKit + Catalyst

    https://developer.apple.com/documentation/uikit/uititlebar

        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            guard let windowScene = (scene as? UIWindowScene) else { return }
            let window = UIWindow(windowScene: windowScene)
    
            if let titlebar = windowScene.titlebar {
    
                //toolbar
                let identifier = NSToolbar.Identifier(toolbarIdentifier)
                let toolbar = NSToolbar(identifier: identifier)
                toolbar.allowsUserCustomization = true
                toolbar.centeredItemIdentifier = NSToolbarItem.Identifier(rawValue: centerToolbarIdentifier)
                titlebar.toolbar = toolbar
                titlebar.toolbar?.delegate = self
    
                titlebar.titleVisibility = .hidden
                titlebar.autoHidesToolbarInFullScreen = true
            }
    
            window.makeKeyAndVisible()
    
        }
    
    #if targetEnvironment(macCatalyst)
    let toolbarIdentifier = "com.example.apple-samplecode.toolbar"
    let centerToolbarIdentifier = "com.example.apple-samplecode.centerToolbar"
    let addToolbarIdentifier = "com.example.apple-samplecode.add"
    
    extension SceneDelegate: NSToolbarDelegate {
    
        func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
            if itemIdentifier == NSToolbarItem.Identifier(rawValue: toolbarIdentifier) {
                let group = NSToolbarItemGroup(itemIdentifier: NSToolbarItem.Identifier(rawValue: toolbarIdentifier), titles: ["Solver", "Resistance", "Settings"], selectionMode: .selectOne, labels: ["section1", "section2", "section3"], target: self, action: #selector(toolbarGroupSelectionChanged))
    
                group.setSelected(true, at: 0)
    
                return group
            }
    
            if itemIdentifier == NSToolbarItem.Identifier(rawValue: centerToolbarIdentifier) {
                let group = NSToolbarItemGroup(itemIdentifier: NSToolbarItem.Identifier(rawValue: centerToolbarIdentifier), titles: ["Solver1", "Resistance1", "Settings1"], selectionMode: .selectOne, labels: ["section1", "section2", "section3"], target: self, action: #selector(toolbarGroupSelectionChanged))
    
                group.setSelected(true, at: 0)
    
                return group
            }
    
            if itemIdentifier == NSToolbarItem.Identifier(rawValue: addToolbarIdentifier) {
                let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.add(sender:)))
                let button = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: barButtonItem)
                return button
            }
    
            return nil
        }
    
        @objc func toolbarGroupSelectionChanged(sender: NSToolbarItemGroup) {
            print("selection changed to index: \(sender.selectedIndex)")
        }
    
        @objc func add(sender: UIBarButtonItem) {
            print("add clicked")
        }
    
        func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
            [NSToolbarItem.Identifier(rawValue: toolbarIdentifier), NSToolbarItem.Identifier(rawValue: centerToolbarIdentifier), NSToolbarItem.Identifier.flexibleSpace,
                NSToolbarItem.Identifier(rawValue: addToolbarIdentifier),
                NSToolbarItem.Identifier(rawValue: addToolbarIdentifier)]
        }
    
        func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
            self.toolbarDefaultItemIdentifiers(toolbar)
        }
    
    }
    #endif
    

提交回复
热议问题