【问题标题】:Toolbar Spacer not working in 3 panel design工具栏垫片在 3 面板设计中不起作用
【发布时间】:2021-11-28 20:20:04
【问题描述】:

我有一个 3 面板 SwiftUI macOS 应用程序(运行 macOS Monterey Beta 9、Xcode 13 Beta 5),我正在向窗口添加工具栏项。

其中一些工具栏项目之间有Spacer,但我得到了这种奇怪的行为: 在侧边栏的工具栏区域,两个按钮之间有一个Spacer,但似乎不起作用。而在详细信息面板中,Spacer 只是半工作状态。

这里是示例代码:

var body: some View {
    NavigationView {
        Text("Sidebar")
            .frame(minWidth: 200)
            .toolbar {
                Button(action: {}) { Image(systemName: "sidebar.left") }
                Spacer() // Comment out this line, and detail panel works fine
                Button(action: {}) { Image(systemName: "play.fill") }
            }
        Text("Middle")
            .frame(minWidth: 200)
            .toolbar {
                Button(action: {}) { Image(systemName: "trash") }
            }
            .navigationTitle("Test")
        Text("Detail")
            .frame(minWidth: 200)
            .toolbar {
                Button(action: {}) { Image(systemName: "scribble") }
                Spacer() // Comment out this line, and sidebar panel works fine
                Button(action: {}) { Image(systemName: "ellipsis.circle") }
            }
    }
}

奇怪的是,从侧边栏中删除Spacer 似乎可以解决详细信息面板上的问题。这也可以反过来工作,因此从详细信息面板中删除 Spacer 会修复侧边栏。

这是预期的行为(我编辑了图像):

这可能是什么问题?

【问题讨论】:

  • 我可以重现它,但没有解决方案。 toolbar macOS 上的奇怪野兽。例如,如果您尝试调整中间列的大小,您会看到 toolbar 段和它下面的视图实际上并没有保持相同的宽度。这一切对我来说都很奇怪......
  • @jnpdx 100%,我遇到了很多奇怪的 toolbar 行为。

标签: swift macos swiftui toolbar


【解决方案1】:

你可以试试这样的:

struct ContentView : View {
    
    var body: some View {
        NavigationView {
            Text("Sidebar").frame(minWidth: 200)
                .toolbar {
                    ToolbarItemGroup (placement: .principal) {
                        HStack {
                            Button(action: {}) { Image(systemName: "sidebar.left") }
                            Spacer()
                            Button(action: {}) { Image(systemName: "play.fill") }
                        }
                    }
                }
            Text("Middle").frame(minWidth: 200)
                .toolbar {
                   ToolbarItemGroup (placement: .principal) {
                    HStack {
                        Text("Test").bold()
                        Spacer()
                        Button(action: {}) { Image(systemName: "trash") }
                    }
                   }
                }
            Text("Detail").frame(minWidth: 200)
                .toolbar {
                    ToolbarItemGroup (placement: .principal) {
                        HStack {
                            Button(action: {}) { Image(systemName: "scribble") }
                            Spacer()
                            Button(action: {}) { Image(systemName: "ellipsis.circle") }
                        }
                    }
                }
        }
    }
}

【讨论】:

  • 如果我删除 .border(.red) 类型代码,除非您安装了扩展程序,否则该代码将立即崩溃,并出现错误“[__NSArrayM objectAtIndex:]: index 9223372036854775806 beyond bounds [ 0 .. 3]”在 macOS 11.3 上。这似乎是将所有 3 个toolbar 组设置为.principal 的结果。如果我删除它,它就会运行。
  • 我将删除仅用于测试的边框。对我来说效果很好,有或没有边界。但是,我在 Monterey Beta 9 上,如问题所示,使用 Xcode 13-beta,而不是发布。旧系统上的行为可能会大不相同。
  • @workingdog 感谢您的回答!但是,我不希望将所有工具栏项都放在主要区域中。我更新了问题以包含预期的行为。
  • 您展示的第二张图片几乎就是我的回答为我带来的。你的系统/目标有什么不同吗?
  • 更正:我的错误,我一直在使用 MacCatalyst 在 macos Monterey 上测试我的代码。这与仅在 macOS 上的应用程序看起来非常不同。抱歉,如果你愿意,我可以删除我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多