首页 > 解决方案 > 在 Mac Catalyst 中更改 NSToolbarItem 图像大小

问题描述

NSToolbarItem使用添加自定义时,NSToolbarDelegate我无法设置图标的图像。使用具有不同大小的默认值(SplitView 或 Share)时会出现问题:具有不同图像大小的 NSToolbarItem 示例

这是其中两个项目的示例代码,一个是共享一个,另一个是添加一个新项目。另请注意,在我正在使用的共享示例中NSSharingServicePickerToolbarItem,即使我正在覆盖它仍在使用“系统”图像的图像。

        case .new:
            let item = NSToolbarItem(itemIdentifier: itemIdentifier)
            item.image = UIImage(systemName: "plus")
            item.label = "New Card"
            item.action = #selector(ViewController.didTapCreateNewToolbarButton(_:))
            item.target = nil
            toolbarItem = item
        case .share:
            let item = NSSharingServicePickerToolbarItem(itemIdentifier: itemIdentifier)
            item.image = UIImage(systemName: "square.and.arrow.up")
            item.label = "Share"
            item.action = #selector(ViewController.didTapShareToolbarButton(_:))
            item.target = nil
            
            toolbarItem = item

我试过使用UIImage.SymbolConfiguration(pointSize: 8, weight: .medium),但结果是一样的。我也尝试过这个解决方案:https ://developer.apple.com/forums/thread/131364但结果是图像占用了整个可用空间(更大且像素化)。

那么,如何更改这些按钮的大小/样式?能够同时更改系统和我的自定义系统会很棒。

编辑 1:此问题仅在删除标题时出现,这使工具栏的高度更小,并正确调整系统按钮的大小,而不是其他按钮:

if let titlebar = windowScene.titlebar {
  titlebar.toolbar = toolbar
  titlebar.toolbarStyle = .automatic
  titlebar.titleVisibility = .hidden // <-- THIS
}

标签: swiftmacosipadmac-catalyst

解决方案


推荐阅读