Amm*_*mad 13 menu ios swift swiftui
我尝试使用 SwiftUI 创建一个包含主标签和辅助标签的按钮的菜单,如下图所示,因此我将按钮和文本(作为辅助标签)嵌入到 VStack 中,但它不起作用。我如何在 swiftUI 中创建这个视图?
代码
Menu {
Section {
search
}
Section {
// The sort button
Menu {
Picker("Sorting options", selection: $sort) {
ForEach(SortType.allCases, id: \.self) { type in
Label("title", systemImage: "chevron.up")
.tag(type)
}
}
} label: {
// Here I should embed title and subtitle like the photo but I don't know how.
Label("Sort By", systemImage: "arrow.up.arrow.down")
}
}
} label: {
menuLabel
}
Run Code Online (Sandbox Code Playgroud)
如果您尝试获得类似于下面的行为,请使用contenta 的闭包Button
Menu("HI") {
Button(action: {}) {
Text(temperatureType.localizedDescription)
Text(formatTemperature(sensor.temperature, to:
temperatureType.toUnitTemperature()))
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我模仿了 iOS 16 中提供的新数据检测器功能,UITextView然后您可以添加任何您想要的修饰符
我找到了一种部分方法来做到这一点,但它没有较小的字幕文本并且在foregroundStyle(.secondary):
Picker(selection: $pointsSortType) {
Button(PointsSortType.date .displayName) {}.tag(PointsSortType.date .rawValue)
Button(PointsSortType.type .displayName) {}.tag(PointsSortType.type .rawValue)
Button(PointsSortType.submissionStatus.displayName) {}.tag(PointsSortType.submissionStatus.rawValue)
Button(PointsSortType.duration .displayName) {}.tag(PointsSortType.duration .rawValue)
} label: {
Label {
Text("""
Sort By
Manual
""")
} icon: {
Image(systemName: "arrow.up.arrow.down")
}
}
.pickerStyle(.menu)
Run Code Online (Sandbox Code Playgroud)
在这里,我们使用多行字符串来欺骗它。我尝试Text在一堆Stacks 中添加多个 s 。我什至尝试使用 来擦除它AnyView(erasing:),但不知怎的,它仍然更聪明。我什至尝试插入Text视图并添加Text诸如粗体之类的修饰符,但它也覆盖了这一点。
抱歉中间有空格,但这就是我喜欢格式化代码的方式。
小智 2
从 iOS 15.0 开始,您可以使用 UIKit 来实现此目的
var menu = UIMenu()
if #available(iOS 15.0, *) {
menu = UIMenu(title: "Sort By",
subtitle: "Name",
children: provideJobSortOptions())
} else {
menu = UIMenu(title: "Sort By"), children: provideJobSortOptions())
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2498 次 |
| 最近记录: |