如何解决如何使用SwiftUI将项目添加到NSToolbar?
考虑此具有应用程序委托生命周期的macOS应用程序代码:
应用委托人:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let contentView = ContentView()
window = NSWindow(
contentRect: NSRect(x: 0,y: 0,width: 480,height: 300),styleMask: [.titled,.closable,.miniaturizable,.resizable,.fullSizeContentView],backing: .buffered,defer: false)
window.toolbar = NSToolbar(identifier: "toolbar")
window.title = "hello title"
window.subtitle = "hello subtitle"
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
SwiftUI:
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(
destination: ItemView(),label: {
Label("Something",systemImage: "folder.circle")
})
NavigationLink(
destination: ItemView(),label: {
Label("Another",systemImage: "pencil.tip.crop.circle")
})
}.listStyle(SidebarListStyle())
}
.frame(maxWidth: .infinity,maxHeight: .infinity)
.toolbar {
ToolbarItem {
Button(action: {
print("Button clicked")
},label: {
Label("Hello",systemImage: "pencil.circle")
})
}
}
}
}
struct ItemView: View {
var body: some View {
Text("Hello,World!")
.frame(maxWidth: .infinity,maxHeight: .infinity)
.toolbar {
ToolbarItem {
Button(action: {
print("Item button")
},label: {
Label("Itemtool",systemImage: "lock.doc")
})
}
}
}
}
这是什么:正确设置窗口和工具栏的外观。工具栏上没有按钮。看起来像这样:
我想做的事情:将从SwiftUI添加的工具栏项添加到工具栏。
当我由SwiftUI应用程序生命周期管理窗口时,一切都按预期工作。但是,出于无关的原因,我需要坚持使用应用程序委托生命周期。
这可能吗?如何将SwiftUI中的工具栏按钮添加到由应用程序委托创建的窗口的工具栏中?
解决方法
在macOS 11 beta 6和Xcode 12 beta 6上:
ContentView:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(
destination: ItemView(),label: {
Label("Something",systemImage: "folder.circle")
})
NavigationLink(
destination: ItemView(),label: {
Label("Another",systemImage: "pencil.tip.crop.circle")
})
}.listStyle(SidebarListStyle())
.toolbar {
ToolbarItem {
Button(action: {
print("Button clicked")
},label: {
Label("Hello",systemImage: "pencil.circle")
})
}
}
}
.frame(maxWidth: .infinity,maxHeight: .infinity)
/* move into the NavigationView
.toolbar {
ToolbarItem {
Button(action: {
print("Button clicked")
},label: {
Label("Hello",systemImage: "pencil.circle")
})
}
}
*/
}
}
struct ItemView: View {
var body: some View {
Text("Hello,World!")
.frame(maxWidth: .infinity,maxHeight: .infinity)
.navigationTitle("hello title") // add title
.navigationSubtitle("hello subtitle") // add sub title
.toolbar {
ToolbarItem {
Button(action: {
print("Item button")
},label: {
Label("Itemtool",systemImage: "lock.doc")
})
}
}
}
}
并且您需要Appdelegate的类:
class AppDelegate: NSObject,NSApplicationDelegate,NSWindowDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// do something
}
// others func
}
最后一个:
@main
struct SomeApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。