SwiftUI macOS 应用程序会更改焦点上的侧边栏高度吗?

eiv*_*dml 6 macos swift swiftui

我在 SwiftUI macOS 应用程序中遇到侧边栏问题。当应用程序首次启动时,没有焦点,侧边栏中有一个额外的顶部填充,当窗口获得焦点时,它会折叠到正确的大小。这对我来说似乎是一个错误。有什么办法可以解决这个问题,还是我做错了什么?

\n

之前(无焦点)

\n

在此输入图像描述

\n

之后(焦点)

\n

在此输入图像描述

\n

正如您所看到的,在这两种情况下,“收件箱”和三个红绿灯点之间的间距是不同的。

\n

在此输入图像描述

\n

代码

\n

这是侧边栏的代码:

\n
NavigationView {\n    List {\n      Text("Inbox")\n      \n      Section(header: Text("All projects")) {\n        ForEachStore(\n          self.store.scope(state: { $0.projects }, action: AppAction.project(id:action:)), content: { projectStore in\n            WithViewStore(projectStore) { projectViewStore in\n              NavigationLink(\n                destination: ProjectView(store: projectStore),\n                label: {\n                  Text(projectViewStore.title)\n                })\n            }\n          }\n        )\n        Spacer()\n      }\n    }.frame(minWidth: 160)\n    .listStyle(SidebarListStyle())\n    HStack {     \n      HStack {\n        Button("\xe2\x88\x92") { viewStore.send(.decrementButtonTapped) }\n        Text("\\(viewStore.count)")\n        Button("+") { viewStore.send(.incrementButtonTapped) }\n        \n      }\n    } \n  }\n
Run Code Online (Sandbox Code Playgroud)\n

eiv*_*dml 0

在一些帮助下我想通了。这是一个可行的解决方案。

NavigationView {
        List {
          Section(header: Text("All projects")) {
            ForEachStore(
              self.store.scope(
                state: { $0.projects },
                action: AppAction.project(id:action:)),
              content: { projectStore in
                NavigationLink(
                  destination: ProjectView(store: projectStore).edgesIgnoringSafeArea(.all),
                  label: {
                    EditableListItemView(store: projectStore)  
                  })
              }
            )
          }
        }
        .overlay(SidebarBottomView(store: store), alignment: .bottom)
        .frame(minWidth: 160, maxHeight: .infinity)
        .listStyle(SidebarListStyle())
        Text("Nothing here")
        
  }
Run Code Online (Sandbox Code Playgroud)

关键部分是.edgesIgnoringSafeArea(.all)目的地视图。它还必须在视图层次结构中的该“级别”指定。