微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以编程方式填充Swift NSToolBar时出现问题,项目不可见?

如何解决以编程方式填充Swift NSToolBar时出现问题,项目不可见?

我似乎无法在程序化的Swift应用程序中显示工具栏项目,但据我所知,它们不会出现。我建立了一个标准的NSWindow,然后尝试以下操作。我已经设法使File Menus和Status bar menus可以在不需要任何笔尖/ xibs / delegates的情况下工作,并且我希望使用工具栏来完成同样的工作。

let toolbar = NSToolbar()
let itemId = NSToolbarItem.Identifier("Identifier")
var toolbarItem: NSToolbarItem
toolbarItem = NSToolbarItem(itemIdentifier: itemId)
toolbarItem.label = String("Label")
toolbarItem.title = String("Title")
toolbarItem.toolTip = String("help")
toolbarItem.isEnabled = true

toolbar.insertItem(withItemIdentifier: itemId,at: toolbar.items.count)
toolbar.isVisible = true
toolbar.displayMode = .iconAndLabel


window.toolbar = toolbar
window.toolbar?.displayMode = .iconAndLabel

在下面我们可以看到工具栏存在,但是它没有显示任何工具栏项目。

enter image description here

从工具栏中添加代码委托以获取帮助:

class ToolbarDelegate: NSToolbar,NSToolbarDelegate {

var toolbarallowedItemIdentifiersList: [NSToolbarItem.Identifier] = [.print,.space,.showFonts,.showColors,.toggleSidebar,.cloudSharing,.flexibleSpace,.showColors];
var toolbarDefaultItemIdentifiersList: [NSToolbarItem.Identifier] = [.print,.showColors];

func toolbar(_ toolbar: NSToolbar,itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
    return nil
}
func toolbarallowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    return toolbarallowedItemIdentifiersList
}
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
    return toolbarDefaultItemIdentifiersList
}

}

解决方法

Willeke的评论帮助我显示了以前不可见的系统默认工具栏选项,但是另一个问题导致我无法显示自己的自定义工具栏选项。

我在这里macOS Menubar and Toolbar without storyboard or .xib menu bar in Swift 5 - Ryan Theodore The中遵循的示例代码在工具栏委托中没有使用func工具栏。因此,实例化之后进行的任何添加尝试都将被忽略。其次,将项目添加到工具栏时,请按标识符进行操作。问题是,标识符在工具栏项本身上没有任何信息。因此,我需要为我需要添加的每个标识符提供该信息。

这给我留下了如下的东西。我创建了两个字典,一个字典用于将字符串名称存储到标识符引用,另一个字典中包含用于工具栏引用的标识符。

然后在下面调用,将新的工具栏项目添加到列表中,然后将项目的标识符插入工具栏(工具栏功能所在的位置),然后使用标识符获取工具栏项目并返回要添加的项目到工具栏。

class ToolbarDelegate: NSToolbar,NSToolbarDelegate {


    var definedToolbarStringIdentifiers: [String:NSToolbarItem.Identifier] = [:]
    var definedToolbarIdentifierItems: [NSToolbarItem.Identifier:NSToolbarItem] = [:]

    func addDefinedToolbarItem(toolBarString: String,toolbarItem: NSToolbarItem) {
        if (!definedToolbarStringIdentifiers.keys.contains(toolBarString)) {
            definedToolbarStringIdentifiers[toolBarString] = toolbarItem.itemIdentifier
            definedToolbarIdentifierItems[toolbarItem.itemIdentifier] = toolbarItem
        }
    }

    var toolbarAllowedItemIdentifiersList: [NSToolbarItem.Identifier] = [.print,.space,.showFonts,.showColors,.toggleSidebar,.cloudSharing,.flexibleSpace,.showColors];
    var toolbarDefaultItemIdentifiersList: [NSToolbarItem.Identifier] = [.print,.showColors];

    func toolbar(_ toolbar: NSToolbar,itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if (self.definedToolbarIdentifierItems.keys.contains(itemIdentifier)) {
            return self.definedToolbarIdentifierItems[itemIdentifier]
        } else {
            return nil
        }
    }
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarAllowedItemIdentifiersList
    }
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return toolbarDefaultItemIdentifiersList
    }
}

然后稍后我调用以下内容:

var toolbarItemB: NSToolbarItem
let itemIdB = NSToolbarItem.Identifier(rawValue: "Testing")
toolbarItemB = NSToolbarItem(itemIdentifier: itemIdB)
toolbarItemB.label = String("Hello Label")
//toolbarItemB.title = String("Hello Title")
toolbarItemB.toolTip = String("Hello help")
toolbar.addDefinedToolbarItem(toolBarString: "Testing",toolbarItem: toolbarItemB)
toolbar.insertItem(withItemIdentifier: itemIdB,at: toolbar.items.count)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。