简体   繁体   中英

MacOS SwiftUI add a button to NSToolbar (not catalyst)

I started building an app using catalyst, liking the idea of being able to use one code for all three apple devices. Problem is I can not have a simple dropdown on the catalyst app so I decided to split the app and use swiftui but for Mac and iOS so that I can have more Mac style controls like a dropdown.

I spent all of last weekend figuring out how to get the tool bar working in catalyst and I finally was able to. So when I ported the app to Mac I go the following error in my toolbar func on the toolbar delegate

Use of unresolved identifier 'UIBarButtonItem'

I spent all night trying to figure out how to add this simple button to the toolbar and got nowhere. The segment control works fine but can't add a button. For now I am just using a segment control (since its working) with one button and text only.

so how do I add a button (and use the system icons) to NSToolBar on a Mac swiftui app?

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if (itemIdentifier == tbi1) {
            let tbi = NSToolbarItemGroup(itemIdentifier: itemIdentifier, titles: ["Accounts","Budgets"], selectionMode: .selectOne, labels: ["One","Two"], target: self, action: #selector(tbi1_click))
            tbi.selectedIndex = 0
            return tbi
        }
        else if (itemIdentifier == tbi2) {
            let bbi1 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(tbi2_click))
            let tbi = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: bbi1)
            return tbi
        }
        return nil
}

I started building an app using catalyst, liking the idea of being able to use one code for all three apple devices. Problem is I can not have a simple dropdown on the catalyst app so I decided to split the app and use swiftui but for Mac and iOS so that I can have more Mac style controls like a dropdown.

I spent all of last weekend figuring out how to get the tool bar working in catalyst and I finally was able to. So when I ported the app to Mac I go the following error in my toolbar func on the toolbar delegate

Use of unresolved identifier 'UIBarButtonItem'

I spent all night trying to figure out how to add this simple button to the toolbar and got nowhere. The segment control works fine but can't add a button. For now I am just using a segment control (since its working) with one button and text only.

so how do I add a button (and use the system icons) to NSToolBar on a Mac swiftui app?

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if (itemIdentifier == tbi1) {
            let tbi = NSToolbarItemGroup(itemIdentifier: itemIdentifier, titles: ["Accounts","Budgets"], selectionMode: .selectOne, labels: ["One","Two"], target: self, action: #selector(tbi1_click))
            tbi.selectedIndex = 0
            return tbi
        }
        else if (itemIdentifier == tbi2) {
            let bbi1 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(tbi2_click))
            let tbi = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: bbi1)
            return tbi
        }
        return nil
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM