在UISplitViewController中使用SwiftUI列表侧边栏 [英] Using a SwiftUI List Sidebar in a UISplitViewController

查看:102
本文介绍了在UISplitViewController中使用SwiftUI列表侧边栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建应用程序的导航,以使我拥有一个UISplitViewController(三列样式),其视图由SwiftUI构建.我的主要补充工具栏目前非常简单:

I am attempting to build my app's navigation such that I have a UISplitViewController (triple column style) with my views built with SwiftUI. My Primary Sidebar is currently quite simple:

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.

where PrimarySidebarSelection is an enum. I am planning to access the same AppModel environment object in my other sidebar, allowing me to change what is displayed in the Supplementary Sidebar, depending on the Primary Selection. I am using the new SwiftUI App life-cycle, rather than an AppDelegate.

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

I would like to know how to change the style of selection from this to the typical sidebar selection style that is used in SwiftUI's NavigationView. According to SwiftUI's List Documentation the selection is only available when the list is in edit mode (and the selection shows the circle next to each item, which I do not want, instead I want the row to highlight like how it does in NavigationView when working with NavigationLinks).

谢谢.

推荐答案

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())
        
    }
}

这篇关于在UISplitViewController中使用SwiftUI列表侧边栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆