在UISplitViewController中使用SwiftUI列表侧边栏



我试图建立我的应用程序的导航,这样我有一个UISplitViewController(三列风格)与我的视图与SwiftUI构建。我的Primary侧边栏目前非常简单:

struct PrimarySidebarView: View {

@EnvironmentObject var appModel: AppModel

var body: some View {
List(PrimarySidebarSelection.allCases, id: .self, selection: $appModel.primarySidebarSelection) { selection in
Text(selection.rawValue)
}
.listStyle(SidebarListStyle())
.navigationBarItems(trailing: EditButton())
}
}

其中PrimarySidebarSelection是一个enum。我计划在我的另一个侧栏中访问相同的AppModel环境对象,允许我根据主要选择更改补充侧栏中显示的内容。我使用的是新的SwiftUI应用生命周期,而不是AppDelegate。

我想知道如何改变选择的样式,从这到典型的侧边栏选择样式,在SwiftUI的NavigationView中使用。根据SwiftUI的列表文档,选择仅在列表处于编辑模式时可用(并且选择显示每个项目旁边的圆圈,这是我不想要的,相反,我希望行像在使用NavigationLinks时在NavigationView中那样突出显示)。

提前感谢。

enum PrimarySidebarSelection: String, CaseIterable {
case a,b,c,d,e,f,g
}
struct SharedSelection: View {
@StateObject var appModel: AppModel = AppModel()
var body: some View {
NavigationView{
PrimarySidebarView().environmentObject(appModel)
Text(appModel.primarySidebarSelection.rawValue)
}
}
}
class AppModel: ObservableObject {
@Published var primarySidebarSelection: PrimarySidebarSelection = .a
}
struct PrimarySidebarView: View {

@EnvironmentObject var appModel: AppModel

var body: some View {
List{
ForEach(PrimarySidebarSelection.allCases, id: .self) { selection in
Button(action: {
appModel.primarySidebarSelection = selection
}, label: {
HStack{
Spacer()
Text(selection.rawValue)
.foregroundColor(selection == appModel.primarySidebarSelection ? .red : .blue)
Spacer()
}
}
)
.listRowBackground(selection == appModel.primarySidebarSelection ? Color(UIColor.tertiarySystemBackground) : Color(UIColor.secondarySystemBackground))
}

}
.listStyle(SidebarListStyle())
.navigationBarItems(trailing: EditButton())

}
}

最新更新