【问题标题】:Using a SwiftUI List Sidebar in a UISplitViewController在 UISplitViewController 中使用 SwiftUI 列表侧边栏
【发布时间】:2021-02-13 23:18:33
【问题描述】:

我正在尝试构建我的应用程序的导航,以便我有一个 UISplitViewController(三列样式)和我使用 SwiftUI 构建的视图。我的主要侧边栏目前非常简单:

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 是一个枚举。我计划在另一个侧边栏中访问相同的 AppModel 环境对象,允许我根据主要选择更改补充侧边栏中显示的内容。我正在使用新的 SwiftUI App 生命周期,而不是 AppDelegate。

我想知道如何将选择样式从这里更改为 SwiftUI 的 NavigationView 中使用的典型侧边栏选择样式。根据SwiftUI's List Documentation,该选择仅在列表处于编辑模式时可用(并且选择显示每个项目旁边的圆圈,这是我不想要的,而是我希望该行像在 NavigationView 中一样在工作时突出显示与 NavigationLinks)。

提前致谢。

【问题讨论】:

    标签: swift swiftui uisplitviewcontroller sidebar ios14


    【解决方案1】:
    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())
            
        }
    }
    

    【讨论】:

    • 我做了一些与此非常相似的事情,我只是希望能够拥有每个各自平台的默认侧边栏 UI,但我想这现在只能做。
    猜你喜欢
    • 2019-10-27
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2021-04-16
    相关资源
    最近更新 更多