界面生成器分段控件?

Bri*_*tow 3 macos xcode interface-builder

我正在构建一个工具栏,我希望它看起来至少有点像预览版。我想创建上一个/下一个按钮。我认为完成的方式是使用分段控件,每个控件中都有一个图像。问题是我不知道如何获得下面的标签。我可以在整个事物下获得一个居中的标签,但标记分段控件,但我无法在控件的每个部分下获得下一个和上一个标签。

我在这里缺少什么?

谢谢。

cti*_*tze 6

正如 @fr\xc3\xa9d\xc3\xa9ric-blanc 指出的那样:实现这一点的唯一方法是以NSToolbarItemGroup编程方式。

\n\n

诀窍是将 aviewsubitems数组都分配给NSToolbarItemGroup.

\n\n
let group = NSToolbarItemGroup(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem"))\n\nlet itemA = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "PrevToolbarItem"))\nitemA.label = "Prev"\nlet itemB = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NextToolbarItem"))\nitemB.label = "Next"\n\nlet segmented = NSSegmentedControl(frame: NSRect(x: 0, y: 0, width: 85, height: 40))\nsegmented.segmentStyle = .texturedRounded\nsegmented.trackingMode = .momentary\nsegmented.segmentCount = 2\n// Don\'t set a label: these would appear inside the button\nsegmented.setImage(NSImage(named: NSImage.goLeftTemplateName)!, forSegment: 0)\nsegmented.setWidth(40, forSegment: 0)\nsegmented.setImage(NSImage(named: NSImage.goRightTemplateName)!, forSegment: 1)\nsegmented.setWidth(40, forSegment: 1)\n\n// `group.label` would overwrite segment labels\ngroup.paletteLabel = "Navigation"\ngroup.subitems = [itemA, itemB]\ngroup.view = segmented\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您想尝试一下,请参阅下面的示例应用程序代码。

\n\n

示例应用程序(针对 Swift 5.1 进行了更新)

\n\n

样品窗口

\n\n

这就是您在新的 macOS/Cocoa 示例应用程序中测试所需的全部内容:

\n\n
    \n
  1. 在 Xcode 中,使用默认选项(Swift、故事板)创建一个新的 macOS 项目。
  2. \n
  3. 添加一个名为的新文件WindowController.swift并将此代码粘贴到其中。
  4. \n
  5. 将界面生成器中的窗口控制器的类设置为WindowController.
  6. \n
\n\n
import Cocoa\n\nclass WindowController: NSWindowController, NSToolbarDelegate {\n\n    var _toolbar: NSToolbar!\n\n    let toolbarItems: [[String: Any]] = [\n        ["title" : "irrelevant :)", "icon": "NSPreferencesGeneral", "identifier": NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem")],\n        ["title" : "Share", "icon": NSImage.shareTemplateName, "identifier": NSToolbarItem.Identifier(rawValue: "ShareToolbarItem")],\n        ["title" : "Add", "icon": NSImage.addTemplateName, "identifier": NSToolbarItem.Identifier(rawValue: "AddToolbarItem")]\n    ]\n\n    var toolbarTabsIdentifiers: [NSToolbarItem.Identifier] {\n\n        return toolbarItems.compactMap { $0["identifier"] as? NSToolbarItem.Identifier }\n\n    }\n\n    override func windowDidLoad() {\n\n        _toolbar = NSToolbar(identifier: "TheToolbarIdentifier")\n        _toolbar.allowsUserCustomization = true\n        _toolbar.delegate = self\n        window?.toolbar = _toolbar\n\n    }\n\n    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {\n\n        guard let infoDictionary: [String : Any] = toolbarItems.filter({ $0["identifier"] as? NSToolbarItem.Identifier == itemIdentifier }).first\n            else { return nil }\n\n        let toolbarItem: NSToolbarItem\n\n        if itemIdentifier == NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem") {\n\n            let group = NSToolbarItemGroup(itemIdentifier: itemIdentifier)\n\n            let itemA = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "PrevToolbarItem"))\n            itemA.label = "Prev"\n            let itemB = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NextToolbarItem"))\n            itemB.label = "Next"\n\n            let segmented = NSSegmentedControl(frame: NSRect(x: 0, y: 0, width: 85, height: 40))\n            segmented.segmentStyle = .texturedRounded\n            segmented.trackingMode = .momentary\n            segmented.segmentCount = 2\n            // Don\'t set a label: these would appear inside the button\n            segmented.setImage(NSImage(named: NSImage.goLeftTemplateName)!, forSegment: 0)\n            segmented.setWidth(40, forSegment: 0)\n            segmented.setImage(NSImage(named: NSImage.goRightTemplateName)!, forSegment: 1)\n            segmented.setWidth(40, forSegment: 1)\n\n            // `group.label` would overwrite segment labels\n            group.paletteLabel = "Navigation"\n            group.subitems = [itemA, itemB]\n            group.view = segmented\n\n            toolbarItem = group\n        } else {\n            toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: itemIdentifier.rawValue))\n            toolbarItem.label = (infoDictionary["title"] as? String)!\n\n            let iconImage = NSImage(named: (infoDictionary["icon"] as? String)!)\n            let button = NSButton(frame: NSRect(x: 0, y: 0, width: 40, height: 40))\n            button.title = ""\n            button.image = iconImage\n            button.bezelStyle = .texturedRounded\n            toolbarItem.view = button\n        }\n\n        return toolbarItem\n    }\n\n    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {\n\n        return self.toolbarTabsIdentifiers\n\n    }\n\n    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {\n\n        return self.toolbarDefaultItemIdentifiers(toolbar)\n\n    }\n\n    func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {\n\n        return self.toolbarDefaultItemIdentifiers(toolbar)\n\n    }\n\n    func toolbarWillAddItem(_ notification: Notification) {\n\n        print("toolbarWillAddItem", (notification.userInfo?["item"] as? NSToolbarItem)?.itemIdentifier ?? "")\n\n    }\n\n    func toolbarDidRemoveItem(_ notification: Notification) {\n\n        print("toolbarDidRemoveItem", (notification.userInfo?["item"] as? NSToolbarItem)?.itemIdentifier ?? "")\n\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n