gpt4 book ai didi

xcode - 界面生成器分段控件?

转载 作者:行者123 更新时间:2023-12-04 03:19:54 26 4
gpt4 key购买 nike

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

我在这里错过了什么?

谢谢。

最佳答案

正如@frédéric-blanc 所指出的:实现这一目标的唯一方法是以编程方式使用 NSToolbarItemGroup

技巧是将 viewsubitems 数组分配给 NSToolbarItemGroup

let group = NSToolbarItemGroup(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem"))

let itemA = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "PrevToolbarItem"))
itemA.label = "Prev"
let itemB = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NextToolbarItem"))
itemB.label = "Next"

let segmented = NSSegmentedControl(frame: NSRect(x: 0, y: 0, width: 85, height: 40))
segmented.segmentStyle = .texturedRounded
segmented.trackingMode = .momentary
segmented.segmentCount = 2
// Don't set a label: these would appear inside the button
segmented.setImage(NSImage(named: NSImage.goLeftTemplateName)!, forSegment: 0)
segmented.setWidth(40, forSegment: 0)
segmented.setImage(NSImage(named: NSImage.goRightTemplateName)!, forSegment: 1)
segmented.setWidth(40, forSegment: 1)

// `group.label` would overwrite segment labels
group.paletteLabel = "Navigation"
group.subitems = [itemA, itemB]
group.view = segmented

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

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

Sample window

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

  1. 在 Xcode 中,使用默认选项(Swift、 Storyboard)创建一个新的 macOS 项目。
  2. 添加一个名为 WindowController.swift 的新文件并将此代码粘贴到其中。
  3. 在界面生成器中将窗口 Controller 的类设置为 WindowController
import Cocoa

class WindowController: NSWindowController, NSToolbarDelegate {

var _toolbar: NSToolbar!

let toolbarItems: [[String: Any]] = [
["title" : "irrelevant :)", "icon": "NSPreferencesGeneral", "identifier": NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem")],
["title" : "Share", "icon": NSImage.shareTemplateName, "identifier": NSToolbarItem.Identifier(rawValue: "ShareToolbarItem")],
["title" : "Add", "icon": NSImage.addTemplateName, "identifier": NSToolbarItem.Identifier(rawValue: "AddToolbarItem")]
]

var toolbarTabsIdentifiers: [NSToolbarItem.Identifier] {

return toolbarItems.compactMap { $0["identifier"] as? NSToolbarItem.Identifier }

}

override func windowDidLoad() {

_toolbar = NSToolbar(identifier: "TheToolbarIdentifier")
_toolbar.allowsUserCustomization = true
_toolbar.delegate = self
window?.toolbar = _toolbar

}

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {

guard let infoDictionary: [String : Any] = toolbarItems.filter({ $0["identifier"] as? NSToolbarItem.Identifier == itemIdentifier }).first
else { return nil }

let toolbarItem: NSToolbarItem

if itemIdentifier == NSToolbarItem.Identifier(rawValue: "NavigationGroupToolbarItem") {

let group = NSToolbarItemGroup(itemIdentifier: itemIdentifier)

let itemA = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "PrevToolbarItem"))
itemA.label = "Prev"
let itemB = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "NextToolbarItem"))
itemB.label = "Next"

let segmented = NSSegmentedControl(frame: NSRect(x: 0, y: 0, width: 85, height: 40))
segmented.segmentStyle = .texturedRounded
segmented.trackingMode = .momentary
segmented.segmentCount = 2
// Don't set a label: these would appear inside the button
segmented.setImage(NSImage(named: NSImage.goLeftTemplateName)!, forSegment: 0)
segmented.setWidth(40, forSegment: 0)
segmented.setImage(NSImage(named: NSImage.goRightTemplateName)!, forSegment: 1)
segmented.setWidth(40, forSegment: 1)

// `group.label` would overwrite segment labels
group.paletteLabel = "Navigation"
group.subitems = [itemA, itemB]
group.view = segmented

toolbarItem = group
} else {
toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: itemIdentifier.rawValue))
toolbarItem.label = (infoDictionary["title"] as? String)!

let iconImage = NSImage(named: (infoDictionary["icon"] as? String)!)
let button = NSButton(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
button.title = ""
button.image = iconImage
button.bezelStyle = .texturedRounded
toolbarItem.view = button
}

return toolbarItem
}

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {

return self.toolbarTabsIdentifiers

}

func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {

return self.toolbarDefaultItemIdentifiers(toolbar)

}

func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {

return self.toolbarDefaultItemIdentifiers(toolbar)

}

func toolbarWillAddItem(_ notification: Notification) {

print("toolbarWillAddItem", (notification.userInfo?["item"] as? NSToolbarItem)?.itemIdentifier ?? "")

}

func toolbarDidRemoveItem(_ notification: Notification) {

print("toolbarDidRemoveItem", (notification.userInfo?["item"] as? NSToolbarItem)?.itemIdentifier ?? "")

}

}

关于xcode - 界面生成器分段控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1323204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com