我想使用SwiftUI在macOS应用程序中重新创建一个与苹果Notes应用程序类似的工具栏(我正在使用Xcode 12.3和macOS 11.1):
我的尝试是使用NavigationView来获取主/细节设置(目前我不需要像原始Notes应用程序那样的第三个面板)。我对如何正确显示外观感兴趣,例如工具栏中按钮的背景颜色和行为。我尝试了一些方法,目前我能想到的最好的方法是这样的:
这将产生以下结果:
现在我的问题是:我如何更改工具栏左、右两侧的颜色?我还遇到了工具栏的行为问题。当主面板的尺寸增大时,尽管还有很多空间,但右侧工具栏的按钮会很早就消失:
我应该怎么做才能防止这种情况发生?
![enter image description here](https://istack.dev59.com/LFB9f.webp)
import SwiftUI
@main
struct App_Without_Name_in_Window_Top_AreaApp: App {
var body: some Scene {
WindowGroup("") { // <-- The ("") will remove the app name in the toolbar
ContentView()
}
.windowToolbarStyle(UnifiedCompactWindowToolbarStyle())
}
}
同时,对于内容视图:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Text("Master")
.frame(minWidth: 200, maxWidth: 300, minHeight: 300, alignment: .leading)
.padding()
.toolbar {
ToolbarItem(placement: .status) {
Button(action: {
myToggleSidebar()
}) {
Image(systemName: "sidebar.left")
}
}
}
.presentedWindowToolbarStyle(ExpandedWindowToolbarStyle())
Text("Detail")
.frame(minWidth: 200, alignment: .center)
.toolbar {
ToolbarItem(placement: .navigation) {
Button(action: {
print("Button pressed")
}) {
Image(systemName: "bold.italic.underline")
}
}
ToolbarItem(placement: .navigation) {
Button(action: {
print("Button pressed")
}) {
Image(systemName: "lock")
}
}
}
}
.frame(minWidth: 500, minHeight: 300)
}
}
func myToggleSidebar() {
NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
这将产生以下结果:
![在此输入图片描述](https://istack.dev59.com/HQfec.webp)
![在此输入图片描述](https://istack.dev59.com/Peufy.webp)
.clipped()
似乎有所帮助,但是你可能会遇到其他问题,所以要小心。 :) - Adam