简体   繁体   English

SwiftUI:为什么从任何视图添加的工具栏项目只出现在 macOS 的主工具栏中?

[英]SwiftUI: Why toolbar items added from any view appear only in the main toolbar in macOS?

In my macOS app (SwiftUI project multiplateform), I have a main toolbar that works well.在我的 macOS 应用程序(SwiftUI 项目多平台形式)中,我有一个运行良好的主工具栏。

However, when I loaded a view, in a popover for example, the toolbar items set in the popover view, are added to the main toolbar.但是,当我加载一个视图时,例如在弹出窗口中,弹出窗口视图中设置的工具栏项目被添加到主工具栏。

In this image above, the X icon visible at top belongs to the popover view.在上图中,顶部可见的 X 图标属于弹出视图。 It's added when loading the popover and hidden when leaving the popover.它在加载弹出窗口时添加,在离开弹出窗口时隐藏。

1个

The toolbar code in the popover view:弹出视图中的工具栏代码:

toolbar {
                Button(action: {
                    selectedItemId = nil // set
                },
                    label: { Image(systemName: "x.circle") })
            }

Adding a searchable field will also be added in the main toolbar, and can break completly the toolbar layout.添加可搜索字段也会在主工具栏中添加,并且可以完全打破工具栏布局。

.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always)) {}

How macOS controls the different toolbar items from different views? macOS 如何从不同的视图控制不同的工具栏项? Each new view shoulb be a "window" to control a new toolbar attached to it?每个新视图都应该是一个“窗口”来控制附加到它的新工具栏?

Thanks in advance.提前致谢。

.popover does not support Toolbars. .popover不支持工具栏。
And in a Sheet the toolbar items are placed at the bottom.在工作Sheet中,工具栏项目位于底部。

But you can always include your own controls and buttons in a Popover.但是您始终可以在 Popover 中包含您自己的控件和按钮。 Just don't use .toolbar :只是不要使用.toolbar

struct ContentView: View {
    
    @State private var showSheet = false
    @State private var showPopover = false

    var body: some View {
        
        VStack {
            Button("Show sheet") { showSheet = true }
            Button("Show popover") { showPopover = true}
        }
        .toolbar {
            Button {
            } label: {
                Image(systemName: "house")
            }
        }
        
        .sheet(isPresented: $showSheet) {
            VStack {
                Text("Dummy Sheet")
                List(0..<20) { i in
                    Text("item \(i)")
                }
                .toolbar {
                    Button {
                    } label: {
                        Image(systemName: "x.circle")
                    }
                }
            }
            .padding()
            .frame(minWidth: 200, minHeight: 200)
        }
        
        .popover(isPresented: $showPopover) {
            VStack {
                Text("Dummy Popover")
                List(0..<20) { i in
                    Text("item \(i)")
                }
                Divider()
                Button {
                } label: {
                    Image(systemName: "x.circle")
                }
            }
            .frame(minWidth: 200, minHeight: 200)
            .padding(.vertical)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM