由于屏幕尺寸较小,工具栏项目无法工作

问题描述 投票:0回答:1

我有五个

ToolbarItem
设置为出现在
topBarTrailing
位置。当应用程序在较小的设备上运行或设备的文本大小增加时,2/3 的工具栏项会合并到菜单项中。但是,点击它不会显示预期的弹出窗口,我在调试器中遇到了这个问题。

在没有上下文菜单可见时调用 -[UIContextMenuInteraction updateVisibleMenuWithBlock:]。这不会有任何作用。

struct ToolBarBugTest: View {
    var body: some View {
        Button(action: {}, label: {
            Text("Tap Me")
        })
        .padding()
        .navigationTitle("My Navigation")
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                Button {
                    print("Tapped")
                } label : {
                    Image(systemName: "eye")
                }
            }
            
            ToolbarItem(placement: .topBarTrailing) {
                // same as above
            }
            ToolbarItem(placement: .topBarTrailing) {
                // same as above
            }
            ToolbarItem(placement: .topBarTrailing) {
                // same as above
            }
            ToolbarItem(placement: .topBarTrailing) {
                // same as above
            }
        }
    }
}

我也使用了 NavigationView,但它会继续在工具栏上添加项目,而不制作菜单。

swift swiftui swiftui-navigationstack
1个回答
0
投票

您需要在按钮标签内使用

Label("Eye", systemImage: "eye")
而不是
Image(systemName: "eye")

Button {
    print("Tapped")
} label : {
    Label("Eye label", systemImage: "eye")
}

系统隐藏的所有

ToolbarItem
将在菜单中分组在一起。为了显示该菜单,SwiftUI 需要标签,该标签除了图像之外还被渲染。

© www.soinside.com 2019 - 2024. All rights reserved.