提问人:Alec Coyner 提问时间:11/14/2023 最后编辑:Alec Coyner 更新时间:11/14/2023 访问量:50
菜单中的 SwiftUI 选取器
SwiftUI Picker in Menu
问:
我想在菜单中使用选择器时显示选择,例如在“排序依据”的提醒应用程序中,但我一生都无法弄清楚该怎么做。
这是我想要的:
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))
}
}
答:
0赞
vollkorntomate
11/14/2023
#1
使用 a 作为 your 的标签(请参阅 Swiftui:菜单项或选取器中的多行):Button
Menu
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")
}
}
另一个提示:您可以在 上使用,因此您不必将其包装在 ..pickerStyle(.menu)
Picker
Menu
评论