侧边栏中的Big Sur工具栏项目

问题描述

在Big Sur中,Xcode和Calendar的工具栏项在打开时会停留在侧栏上方,而在折叠时会在左侧显示

侧边栏打开:

enter image description here

侧边栏崩溃:

enter image description here

"Adopt the New Look of macOS"的13:55中,约翰说:“放置在分隔符[sidebarTrackingSeparator]之前的项目将显示在全高度的边栏上方”,就像它们在Xcode和Calendar中一样。我无法完成这项工作。

Here's a sample project演示了此问题。我使用了IB定义的“带有边栏的窗口控制器”,并添加一个工具栏项来切换边栏。在NSWindowController的子类中,我在.sidebarTrackingSeparator项之后插入.toggleSidebar

override func windowDidLoad() {
    // Sometimes the toolbar items aren't loaded yet--async is a quick and dirty way to prevent a crash
    dispatchQueue.main.async {
        self.window?.toolbar?.insertItem(withItemIdentifier: .sidebarTrackingSeparator,at: 1)
    }
}

有时这无效(切换按钮仍在侧栏的右侧)。有时边栏切换会进入溢出菜单

enter image description here

在WWDC会话之外,我还没有看到有关实现此工具栏设计的任何讨论。有人能使它正常工作吗?

解决方法

这是IB /代码定时不一致。在添加.sidebarTrackingSeparator工具栏项目之前,Interface Builder会配置并安装工具栏。

因此,您做对了事,为时已晚。而且稍后进行分派。我认为重要的是在窗口上设置工具栏之前将项目放在那里。

不幸的是,使用IB实际上是不可能的,除非我相信,否则您将创建一个全新的工具栏并重新分配它。但这是一个坏主意,因为这样您可能会遇到麻烦,无法自动保存工具栏的状态。

技巧是在Interface Builder中配置分隔符。如果您查看ObjC documentation for this constant,将会看到一个较长的名称:NSToolbarSidebarTrackingSeparatorItemIdentifier

我们在这里能做的最好的就是希望符号的名称与标识符的值相同。如果您确实要验证这一点,则可以在调试器中打印符号的值:

(lldb) po NSToolbarSidebarTrackingSeparatorItemIdentifier
NSToolbarSidebarTrackingSeparatorItemIdentifier

如果我们在IB中创建一个自定义工具栏项,然后根据John的视频进行添加...

Interface Builder with a custom toolbar item positioned in the toolbar. The item has the identifier NSToolbarSidebarTrackingSeparatorItemIdentifier set in the attributes inspector

低下观察: enter image description here