我有一个无法解决的问题。我在 NavigationView 中有一个带有两个选项卡的 TabView。两个选项卡都以相同的方式构建 SearchBar + List,唯一的区别是内容,但即使我尝试在两者上显示相同的内容也会出现问题。
问题是在第一个选项卡上搜索栏和列表之间有额外的填充,它没有出现在第二个选项卡上。如果我在代码问题中切换顺序,则仅在第一个选项卡(旧的第二个选项卡)上填充相同。
重要说明,它只发生在 .listStyle(SidebarListStyle())
和 .listStyle(PlainListStyle())
中,没有额外的填充,但我无法展开和折叠类别。
代码:
var body: some View {
NavigationView {
TabView {
ProductList()
.tabItem {
Label("Produkty", systemImage: "bag.circle.fill")
}
.onAppear {
self.title = "Produkty"
}
RecipeList()
.tabItem {
Label("Przepisy", systemImage: "book.circle.fill")
}
.onAppear {
self.title = "Przepisy"
}
}
.accentColor(.white)
.navigationBarTitle(title, displayMode: .inline)
}
}
ProductList()
的代码:
import SwiftUI
struct ProductList: View {
@Environment(\.presentationMode) var presentationMode
@State private var searchText = ""
let categories: [String] = ["A", "B", "C"]
var body: some View {
VStack() {
SearchBar(text: $searchText)
.padding(.top, 10)
List(){
ForEach(categories, id: \.self) { cat in
Section(
header: Text(cat)
.fontWeight(.bold)
.foregroundColor(.gray)
){
ForEach(productData.filter{
searchText.isEmpty && $0.category.rawValue == cat ? true : $0.name.localizedCaseInsensitiveContains(searchText) && $0.category.rawValue == cat
}) { product in
NavigationLink(destination: ProductDetail(product: product)){
ProductRow(
product: product,
color: colorRank(value: product.rank[0].rawValue)
)
}
}
}
.accentColor(.gray)
}
}
.resignKeyboardOnDragGesture()
.listStyle(SidebarListStyle())
}.background(Color(UIColor.secondarySystemBackground).ignoresSafeArea())
}
}