ios 菜单中的SwiftUI选取器

0ejtzxu1  于 11个月前  发布在  iOS
关注(0)|答案(1)|浏览(95)

我想在菜单中使用选择器时显示选择,就像在提醒应用程序中的排序方式一样,我无法为我的生活弄清楚如何做到这一点。
这就是我想要的:
https://i.stack.imgur.com/Lwm2R.jpg
下面是我的代码:

private func menuButton() -> some View {
    Menu {
        Menu {
            Picker(selection: $group, label: Text("Grouping options")) {
                Text("None").tag(0)
                Text("Type").tag(1)
            }
        } label: {
            Label("Group By", systemImage: "square.grid.3x1.below.line.grid.1x2")
        }
        Menu {
            Picker(selection: $sort, label: Text("Sorting options")) {
                Text("Name").tag(0)
                Text("Priority").tag(1)
            }
        } label: {
            Label("Sort By", systemImage: "arrow.up.arrow.down")
        }
        Divider()
        Button {
            toggleInbox()
        } label: {
            Label(inboxList?.isHidden == false ? "Hide Inbox" : "Show Inbox", systemImage: inboxList?.isHidden == false ? "eye.slash" : "eye")
        }
    } label: {
        Image(systemName: "ellipsis.circle.fill")
            .font(.custom("Button", size: 22))
            .foregroundStyle(.blue, Color(UIColor.systemGray5))
    }
}

字符串

mum43rcc

mum43rcc1#

使用Button作为Menu的标签(参见Swiftui: multiple lines in a menu item or picker):

Menu {
    Picker(selection: $sort, label: Text("Sorting options")) {
        Text("Name").tag(0)
        Text("Priority").tag(1)
    }
} label: {
    Button(action: {}) {
        Text("Sort by")
        Text(sort == 0 ? "Name" : "Priority")
        Image(systemName: "arrow.up.arrow.down")
    }
}

字符串
另一个提示:您可以在Picker上使用.pickerStyle(.menu),因此不必将其 Package 在Menu中。

相关问题