网上关于定制macOS-toolbar的文章都比较老,这篇文章讲下如何用swiftUI修改toolbar,比较推荐方法二。
方法一:
在Appdelegate文件里加上
let titlebarAccessoryView = TitlebarAccessory().padding([.top, .leading, .trailing], 16.0).edgesIgnoringSafeArea(.all)
let accessoryHostingView = NSHostingView(rootView:titlebarAccessoryView)
accessoryHostingView.frame.size = accessoryHostingView.fittingSize
let titlebarAccessory = NSTitlebarAccessoryViewController()
titlebarAccessory.view = accessoryHostingView
titlebarAccessory.layoutAttribute = .trailing
以及
window.addTitlebarAccessoryViewController(titlebarAccessory)
新建一个TitlebarAccessory.swift的文件,内容如下:
import SwiftUI
struct TitlebarAccessory: View {
var body: some View {
Text("hello")
}
}
运行
完整代码:
let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext).frame(minWidth: 450, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
// Create the titlebar accessory
let titlebarAccessoryView = TitlebarAccessory().padding([.top, .leading, .trailing], 16.0).edgesIgnoringSafeArea(.all)
let accessoryHostingView = NSHostingView(rootView:titlebarAccessoryView)
accessoryHostingView.frame.size = accessoryHostingView.fittingSize
let titlebarAccessory = NSTitlebarAccessoryViewController()
titlebarAccessory.view = accessoryHostingView
titlebarAccessory.layoutAttribute = .trailing
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.setFrame(CGRect(x: 10, y: 10, width: 550, height: 650), display: true)
window.center()
window.addTitlebarAccessoryViewController(titlebarAccessory)
window.backgroundColor = NSColor.white
//window.minSize = NSSize(width: 500, height: 400)
//window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
方法二(推荐):
1、在AppDelegate中,将ContentView设置为无视上方安全区域,这样我们的ContentView就可以直接显示在toolbar区域。也就是说,我们能方便地在ContentView.swift文件中,用swiftui定制我们的toolbar
let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext).edgesIgnoringSafeArea(.top).frame(minWidth: 450, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
2、设置toolbar部分透明
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
3、效果图
4、完整代码
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext).edgesIgnoringSafeArea(.top).frame(minWidth: 450, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .unifiedTitleAndToolbar,.miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
//设置标题栏白色 无分界线
//let customToolbar = NSToolbar()
//customToolbar.showsBaselineSeparator = false
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
//window.backgroundColor = .white
//window.toolbar = customToolbar
window.setFrame(CGRect(x: 10, y: 10, width: 550, height: 650), display: true)
window.center()
//window.addTitlebarAccessoryViewController(titlebarAccessory)
window.backgroundColor = NSColor.white
//window.minSize = NSSize(width: 500, height: 400)
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}