gpt4 book ai didi

macos - 使用 Swift UI 自定义 macOS 工具栏

转载 作者:行者123 更新时间:2023-12-05 08:04:05 24 4
gpt4 key购买 nike

我正在开发一个 macOS 应用程序, View 层是用 SwiftUI 编写的。我知道 iOS 工具栏至少可以更改背景颜色,但是当我尝试在 macOS 中执行此操作时,它的行为并不像我预期的那样。

这是一个(简化的)示例:

struct ContentView: View {
var body: some View {
NavigationView {
Collections()
.layoutPriority(0)

Photos()
.frame(maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)
.background(Color.Alt.black)
.layoutPriority(1)
}
.toolbar {
Toolbar().background(Color.red500)
}
}
}

struct Toolbar: View {
var body: some View {
Group {
Slider(value: 250, in: 150...400) {
Text("Toolbar.PreviewSize")
} minimumValueLabel: {
Image(systemName: "photo").resizable().scaledToFit().frame(width: 15)
} maximumValueLabel: {
Image(systemName: "photo").resizable().scaledToFit().frame(width: 23)
} onEditingChanged: { _ in
// do nothing
}.frame(minWidth: 200)

Spacer()

Text("Toolbar.SelectionCount")

Spacer()

AddPhotosButton()
}
}
}

这会产生类似这样的效果,如您所见,不会将背景色应用到整个工具栏,只是应用到工具栏中的项目:

Toolbar with red background on the toolbar items, but not the entire toolbar

我想我可以制作自己的 WindowToolbarStyle 样式,但是没有关于该协议(protocol)的文档!

如果我将自己的工具栏制作为 View 而不是使用 .toolbar 修饰符,当侧边栏被折叠,导致一团糟:

custom toolbar that works well when the sidebar is open custom toolbar that doesn't work when the sidebar is closed

感谢您的帮助!

最佳答案

我推荐

{
.toolbar {
Toolbar()
}.toolbarBackground(Color.gray)
}

关于macos - 使用 Swift UI 自定义 macOS 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70161675/

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