SwiftUI侧边栏不记得状态 [英] SwiftUI Sidebar doesn't remember state

查看:145
本文介绍了SwiftUI侧边栏不记得状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,它使用iOS14中针对iPad os引入的新侧边栏,但我不知道为什么它不记得隐藏时的状态

I Have this app that uses the new sidebar introduced in iOS14 for iPad os but I can't figure out why it doesn't remember the state when its hidden

这是侧边栏结构

import SwiftUI

struct Sidebar: View {
    
    @Environment(\.managedObjectContext) var moc
    @Binding var selection : Set<NavigationItem>
    
    var body: some View {
        List(selection: $selection) {
            NavigationLink(destination: AgendaView().environment(\.managedObjectContext, moc).navigationTitle("Agenda"), label: {
                Label("Agenda", systemImage: "book")
            })
            .tag(NavigationItem.agenda)
            
            NavigationLink(destination: Text("Subjects"), label: {
                Label("Materie", systemImage: "tray.full")
            })
            .tag(NavigationItem.subjects)
            
            NavigationLink(destination: Text("Calendario"), label: {
                Label("Calendario", systemImage: "calendar")
            })
            .tag(NavigationItem.calendar)
            
            NavigationLink(destination: SettingsView().environment(\.managedObjectContext, moc).navigationTitle("Impostazioni"), label: {
                Label("Impostazioni", systemImage: "gear")
            })
            .tag(NavigationItem.settings)
            
        }
        .listStyle(SidebarListStyle())
    }
}

为了标记元素,我使用了一个名为NavigationItem的自定义结构

for tagging the elements I use a custom struct called NavigationItem

enum NavigationItem {
    case agenda
    case calendar
    case ...
}

这是我在内容视图中放置侧边栏的位置,您可以看到该设备是否是iPad(使用sizeClasses检测到),我使用侧边栏,否则,如果是iPhone,则我使用TabBar

and here is where I placed the Sidebar in the content view, as you can see if the device is an iPad (detected using sizeClasses) I use the sidebar, otherwise if its an iPhone I use the TabBar

import SwiftUI

struct ContentView: View {
    @Environment(\.horizontalSizeClass) var horizontalSizeClass
    @Environment(\.managedObjectContext) var moc
    
    @State private var selection : Set<NavigationItem> = [.agenda]
    
    @ViewBuilder
    var body: some View {
        
        if horizontalSizeClass == .compact {
            TabBar(selection: $selection)
                .environment(\.managedObjectContext, moc)
        } else {
            NavigationView {
                Sidebar(selection: $selection)
                    .environment(\.managedObjectContext, moc)
                    .navigationTitle("Menu")
            }
        }
    }
}

推荐答案

List(selection: $selection)仅用于macOS.看到标题中的以下注释:

List(selection: $selection) for selection binding is macOS only. See this comment in the header:

/// On iOS and tvOS, you must explicitly put the list into edit mode for
/// the selection to apply.

作为解决方法,您可以使之前选择的行显示为粗体,例如

As a workaround you could make the previously selected row appear bold, e.g.

 NavigationLink(
        destination: HomeView(),
        tag: Screen.home,
        selection: $selection,
        label: {
            Label("Home", systemImage: "house" )
        })
        .font(Font.headline.weight(selection == Screen.home ? .bold : .regular))

有关更多信息,请参见 answer .

For more see this answer.

这篇关于SwiftUI侧边栏不记得状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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