【发布时间】:2021-10-13 13:53:00
【问题描述】:
我正在尝试编写一个列表邀请导航视图,该视图也使用searchable:
.searchable(text: $query, placement: .toolbar, prompt: "Search")
我将 Picker 放入的视图如下所示:
struct FolderView: View {
let title: String
let mails: [Mail]
@Binding var selectedMail: Mail?
@State private var selectedColorIndex = 0
var body: some View {
List {
Picker("Favorite Color", selection: $selectedColorIndex, content: {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
})
.pickerStyle(SegmentedPickerStyle())
ForEach(mails) { mail in
NavigationLink(
destination: MailView(mail: mail),
tag: mail,
selection: $selectedMail
) {
VStack(alignment: .leading) {
Text(mail.subject)
Text(mail.date, style: .date)
}
}
}
}.navigationTitle(title)
}
}
屏幕是这样的:
我试图弄清楚如何将分段控件放在搜索栏下方的导航栏中,使其看起来组合在一起而不是列表的一部分...
这将允许我搜索和过滤。取决于选择的片段。
如何将选择器视图放在导航栏内?
按照提供的答案状态,将 Picker 放在 .toolbar 修饰符中,如下所示:
.toolbar {
Picker("Favorite Color", selection: $selectedColorIndex, content: {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
})
.pickerStyle(SegmentedPickerStyle())
}
让它看起来像这样:
我在问题中提到的另一件事是我试图将 Picker 放在搜索栏下方。有没有办法做到这一点? 我是否必须编写自定义搜索视图实现,其中有一个搜索栏和我想要的任何其他视图,才能将选择器放在搜索栏下方?
编辑:
添加显示我如何按要求添加搜索栏的代码:
var body: some View {
NavigationView {
Sidebar(
store: store,
selectedFolder: $selectedLabel,
selectedMail: $selectedMail
)
Text("Select label...")
Text("Select mail...")
}
.searchable(text: $query, placement: .toolbar, prompt: "Search")
}
【问题讨论】:
-
向我们展示您添加搜索视图的位置和方式的代码。然后我们就可以在它下面添加工具栏了。
标签: swift swiftui navigationview